logo

Using a filter by a process context

This article provides an example of using a filter by a business process context. Assume you need to find process instances, in which the variable is 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.

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.13.11 inclusive.

Namespaces:

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

Script text:

var filter = InterfaceActivator.Create<WorkflowInstanceFilter>();//create a filter instance
var header = PublicAPI.Processes.ProcessHeader.Load(31);// load the process to the variable by ID
//var header = PublicAPI.Processes.ProcessHeader.Find("Name = My process'").FirsOrDefault();//Or search by name
//var header = PublicAPI.Processes.ProcessHeader.Find("Token = 'WebToker'").FirsOrDefault();//Or by launching token from external systems if it is specified
var contextFilterType = Locator.GetServiceNotNull<WorkflowInstanceContextService>().GetInstanceContextFilterType(header.Published);// set the context filter type
dynamic fl = Activator.CreateInstance(contextFilterType);// create a context filter instance
fl.SearchString = "string"; // assign a value for filtering to the variable
filter.ContextFilter = fl;// assign the required value to the filter
filter.ProcessHeader = header;    // assign a process to the filter, which was determined by ID
var process = WorkflowInstanceManager.Instance.FindUsingContext(filter, null);// define search in context.
foreach(var p in process)
{
    Console.WriteLine(p.AsDynamic().WorkflowInstance.Name);// display the process instances names
}

Script without PublicAPI

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;

Script text:

var filter = InterfaceActivator.Create<WorkflowInstanceFilter>();//create filter instance
var header = ProcessHeaderManager.Instance.Load(31);//load the process to the context variable by ID
var contextFilterType = Locator.GetServiceNotNull<WorkflowInstanceContextService>().GetInstanceContextFilterType(header.Published);//set the context filter type
dynamic fl = Activator.CreateInstance(contextFilterType);//create a context filter instance
fl.SearchString = "string"; //assign a value for filtering to the variable
filter.ContextFilter = fl;//assign the searched value to the filter
filter.ProcessHeader = header;    //assign the process, obtained by ID, to the filter
var process = WorkflowInstanceManager.Instance.FindUsingContext(filter, null);//define search in context.
foreach(var p in process)
            {
                Console.WriteLine(p.AsDynamic().WorkflowInstance.Name);//display the process instances names
            }

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, even 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:

foreach(var p in process)
{
      p.AsDynamic().WorkflowInstance.Name = "Changed name";//change the name.
}