7

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

Master Yoda
  • 4,334
  • 10
  • 43
  • 77

2 Answers2

1

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.

Master Yoda
  • 4,334
  • 10
  • 43
  • 77
  • do you return data to your main/parent process?, your approach will create new process instances. – Abbas Kararawala Jan 23 '18 at 07:41
  • @Abbas Kararawala no. Once the complete task process is started it becomes it's own instance. You can map it to a parent process but it won't depend on it – Master Yoda Jan 23 '18 at 08:45
  • This approach works better for me as individual processes (tasks) can be aborted at any point rather than having to send a message to the parent to finish the current task. – Master Yoda Jan 23 '18 at 09:50
  • :) the question is how do you map it to the parent process, in case you know – Abbas Kararawala Jan 23 '18 at 11:49
  • 1
    It depends on how your app is set up. In my app I pass the process id from the parent process to the secondary process via the variables overload for `runtimeService.startProcessInstanceByKeyAndTenantId(String processDefinitionKey, Map variables, String tenantId)` After this I just map the current process to the parent using a service task. See my script above for more details – Master Yoda Jan 23 '18 at 11:54
  • FYI you can get the parent process instance id using `execution.getProcessInstanceId();` from within the scope of the parent process. [click here for docs](https://www.activiti.org/javadocs/org/activiti/engine/runtime/Execution.html#getProcessInstanceId--) – Master Yoda Jan 23 '18 at 12:00
  • Is there REST api available to get (nrOfInstances, nrOfActiveInstances & nrOfCompletedInstances) ? Can you please share REST api url – Sundar Jan 30 '19 at 13:12
0
  1. Seems like you are updating only one variable (or a single set of variables) as a result of each task. This will override the previous value. use distinct variables, or append something before each variable to mark it unique for the task/ sub-process completed. see collapsed sub-process

  2. Yes, each sub process gets its own unique execution id, But the main execution ID or process instance ID remains same

Abbas Kararawala
  • 1,254
  • 12
  • 19