Using a dynamic swimlane

Dynamic swimlanes allow you to design flexible processes. Let's say there is a company with several legal entities. In the accounting department, each employee is responsible for a certain legal entity. Consider a process for approval of incoming contracts in the accounting departments. 

Create a new object - Our Company. Add Name and Accountant properties. Then create a contract approval process with a dynamic swimlane for the approver.

To define the swimlane with a script, the swimlane must be dynamic. 

When you change the swimlane type, ELMA Designer will suggest creating a variable for storing the user.

It is important to know that there are two types of dynamic swimlanes: Dynamic (selected from list) and Dynamic (defined by script). You can use either of the two, but the swimlane defined by script allows you to store a script for calculating the variable value. You can add the Script graphic element before the connector going to the required swimlanes, as shown below.

However, for better readability, we advise placing the script within the required swimlane.

 

Although there are cases when the executor must be chosen before the process goes to the following swimlane, for example, if the initiator has to know which employee will be approving the contract before actually assigning the task. 

 
The script for defining the approver allows the users to:
  1. Select the approving accountant manually;
  2. Assign the task to the accountant responsible for the legal entity;
  3. Assign the task to the Chief Accountant if the amount of the contract is higher than (in our example, N is one million);
  4. Assign the task to the Chief Accountant if the accountant responsible for the legal entity has too many tasks at the moment (in our example, more than three approval tasks)
 
When using scripts to define swimlane, make sure that the script will be always executed correctly and on time. Otherwise, the logic of the process might be broken leading to errors in process execution.

Script with PublicAPI

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

Script text:

//if the task author does not select the approver manually, it is selected by script
if (context.Approver == null)
{
    var runningStatus = PublicAPI.Enums.Workflow.WorkflowInstanceStatus.Running;
    //create filter for active process instances
    var filter = PublicAPI.Processes.WorkflowInstance.Filter()
        .Statuses(runningStatus)
        .TaskName("Contract approval").Filter;
    var count = PublicAPI.Processes.WorkflowInstance.Count(filter);
    //if there are more than three active instances or the contract's amount is more than a million, the task is assigned to the the Chief Accountant
    if (count > 3 || context.ContractAmount >= 1000000)
    {
        //for that, define the superior of the accountant assigned to the legal entity 
        var chiefs = context.Organization.Accountant.OrganizationItems.ToArray()
            .Union(context.Organization.Accountant.OrganizationGroups)
            .Select(organizationItem =>
            {
                var parentOrganizationItem = organizationItem.ParentItem;
                while (parentOrganizationItem != null && parentOrganizationItem.User == null)
                    parentOrganizationItem = parentOrganizationItem.ParentItem;
                return parentOrganizationItem != null ? parentOrganizationItem.User : null;
            })
            .Where(u => u != null);
        var chief = chiefs.First();
        //assign the superior to the swimlane
        context.Approver = chief;
    }
    //else assign the task to a regular accountant assigned to the legal entity 
    else
    {
        context.Approver = context.Organization.Accountant;
    }
}

Script without PublicAPI 

Use the following namespaces: 

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

Script text:

//if the task author does not select the approver manually, the approver is selected by the script 
if(context.Approver == null)
{
//create filter for active process instances
var filter = InterfaceActivator.Create<WorkflowInstanceFilter>();
filter.Statuses = new List<WorkflowInstanceStatus>();
//only active
filter.Statuses.Add(WorkflowInstanceStatus.Running);
//only approver's task
filter.TaskName = "Contract approval";
//obtain the number of active process instances according to filter
var count = WorkflowInstanceManager.Instance.Count(filter);
//if there are more than three running instances, or the contract's amount is higher than one million, the task is assigned to the Chief Accountant 
if(count > 3 || context.ContractAmount >= 1000000)
{
//for that, define the superior of the accountant assigned to the legal entity 
var chiefs = context.Organizaciya.Buhgalter.OrganizationItems.ToArray()
.Union(context.Organization.Accountant.OrganizationGroups)
.Select(organizationItem =>
  {
  var parentOrganizationItem = organizationItem.ParentItem;
  while (parentOrganizationItem != null && parentOrganizationItem.User == null)
  parentOrganizationItem = parentOrganizationItem.ParentItem;
  return parentOrganizationItem != null ? parentOrganizationItem.User : null;
  })
.Where(u => u != null);
var chief = chiefs.First();
//assign the supervisor to the swimlane
context.Approver = chief;
}
//else assign the task to the regular accountant assigned to the legal entity
else
{
context.Approver = context.Organization.Accountant;
}