Using a filter by a process context

This article provides and example of using a filter by a business process context. Assume you need to find process instances, in which the variable context.Number = 10. Note, that the Participates in search box must be checked for the Number context variable in the process you are going to search by.

For the script to work, add the global assembly

Microsoft.CSharp

and the following namespaces:

using EleWise.ELMA.Extensions;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Services;
using EleWise.ELMA.Workflow.Managers;
using EleWise.ELMA.Workflow.Models;
 

Process instances are searched with the script:

var filter = InterfaceActivator.Create<WorkflowInstanceFilter>();//create a filter instance
var header = ProcessHeaderManager.Instance.Load(31);//load the process to the variable depending on id
var contextFilterType = Locator.GetServiceNotNull<WorkflowInstanceContextService>().GetInstanceContextFilterType(header.Published);//set the context filter type
dynamic fl = Activator.CreateInstance(contextFilterType);//create a filter instance by context
fl.SearchString = "string"; //set the variable value to filter by
filter.ContextFilter = fl;//set the searched value to the filter
filter.ProcessHeader = header;    //set the process defined by ID to the filter
var process = WorkflowInstanceManager.Instance.FindUsingContext(filter, null);//set search by context.
process.ToList().ForEach(p=>{Console.WriteLine(p.AsDynamic().WorkflowInstance.Name);});//write the process instances found by the filter to the variable
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
 
var header = PublicAPI.Processes.ProcessHeader.Load(31);//load the process to the variable depending on id
var contextFilterType = Locator.GetServiceNotNull<WorkflowInstanceContextService>().GetInstanceContextFilterType(header.Published);//set the context filter type
dynamic fl = Activator.CreateInstance(contextFilterType);//create a filter instance by context
fl.SearchString = "string"; //set the variable value to filter by
var filter = PublicAPI.Processes.WorkflowInstance.Filter().ProcessHeader(header).ContextFilter(fl).Filter;
var process = WorkflowInstanceManager.Instance.FindUsingContext(filter, null);//set search by context.
process.ToList().ForEach(p => { Console.WriteLine(p.AsDynamic().WorkflowInstance.Name); });//write the process instances found by the filter to the variable

Keep in mind, that after the execution of this script, the list of objects matching the filter parameters will be written to the Process variable. Often you need to work with this list as with a dynamic set of data (the system perceives the data in the variable as a list, event if only one instance satisfies the filter). Later you can add a cycle for working with lists and work with a specific process instance, for example:

process.ToList().ForEach(p=>p.AsDynamic().WorkflowInstance.Name = "Changed name"; ); //change the name.