Interrupting a timer in a business process with a script
Say, you need to implement the following logic in a business process: a task in the process has to be assigned to the head of a department. If the department´s head does not complete this task in 4 hours, it will be automatically reassigned to the substitute user. Fig.1 illustrates a part of the graphic model of the process:
In this case, a parallel gateway ensures simultaneous execution of two process paths: when the head of the department receives the task, the timer is set for four hours. When the time runs out, a script is executed and checks the status of the task. If the task hasn't been completed, the script reassigns it to the substitute user. If the head completes the task in time, the next steps of the process have to be executed, i.e. the timer has to be interrupted so that the process did not wait for four hours to check the task status.
Here is an example of the script, which interrupts the timer:
var instances = new []{context.WorkflowInstance};
var allInstanceTimers = Locator.GetServiceNotNull<WorkflowInstanceManager>().GetActiveTimerInfos(instances);
//search a timer by its name in the graphic model
var t = allInstanceTimers.ToList().Find(a => a.ElementName == "Event 1");
//output the name to the context variable
context.Str = t.ElementName;
var job = EntityManager<IResumeProcessSchedulerJob>.Instance.Load(t.SchedulerJobId);
if (job.Task != null && job.Task.Status == ELMA.Scheduling.Models.SchedulerTaskStatus.Enabled)
{
//execute the timer at the moment, i.e. interrupt it
job.Task.OnceExecuteTime = DateTime.Now;
job.Task.Save();
}
Use the following namespaces in the script:
using EleWise.ELMA.Services;
using EleWise.ELMA.Workflow.Managers;
using EleWise.ELMA.Workflow.Models;
using EleWise.ELMA.Model.Managers;
Note that starting with ELMA 3.2.0 you can implement such a logic easier, as shown in fig. 2:
Fig.2
In this case, the escalation mechanism is used. To learn more, check this article "Process Escalation". Here the timer escalation is used: if the head of the department does not complete the task in time, the business process automatically moves to the substitute user’s task, located in the respective swimlane.