Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alfresco Activiti - Create multiple instances of the same subprocess

I have what seemed like a fairly simple requirement for a process that im beginning to question is even possible.

The image below shows my current process. I am trying to achieve two things:

  1. A user creates an initial user task for adding a note, they should be able to add as many notes as they wish with one user task per note

  2. A new sub-process is spawned for each new note (user task) that the user has created.

Example Process

The process above presents the following problems:

  • A sub-process should be spawned for each task, however they seem to overwrite each other

  • Im not sure if the sub-process requires a unique id for each new sub-process spawned

like image 412
Master Yoda Avatar asked Jan 13 '18 19:01

Master Yoda


1 Answers

So it turns out that the solution to this question requires a bit of scripting using groovy.

Below is the updated process model diagram, in it I start a new instance of the Complete Task process using a script task then if the user wishes to add more tasks the exclusive gateway can return the user to the Create task (user task) OR finish the process.

I clear down any values in the fields held within the user task within the script task before I pass the scope back to the user task.

Update Process

The image below shows my Complete Task process that gets called by the main process using a script

Complete Task

Here I avoid using parallel gateways in preference of creating a new instance of the Create Task (user task) and a new instance of the Complete task process (not subprocess) via means of the script.

To start a new instance of the Complete Task process we have to start the process using the function startProcessInstanceByKeyAndTenantId() under a runtimeService instance for the process, although I could also use startProcessInstanceByIdAndTenantId():

//Import required libraries
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

//instantiate RunTimeService instance
RuntimeService runtimeService = execution.getEngineServices().getRuntimeService();

//get tenant id
String tenantId = execution.getTenantId(); 

//variables Map
Map<String, Object> variables = runtimeService.getVariablesLocal(execution.getProcessInstanceId());

//start process (processId, variables, tenantId)
ProcessInstance completeTask = runtimeService.startProcessInstanceByKeyAndTenantId("CompleteTask", variables, tenantId);

//Clear variables to create a fresh task
execution.setVariable("title", "");
execution.setVariable("details", "");

Using this approach I avoid creating multiple subprocesses from the parent process and instead create multiple processes that run separate from the parent process. This benefits me as if the parent process completes the others continue to run.

like image 59
Master Yoda Avatar answered Sep 24 '22 02:09

Master Yoda