Camunda 7- Modifying Multi-instance - Add instance and variables to it

Hi Members,

I have running process instance which has multi instance loop(initial count was 4). Multi instance loop has sub-flow in it. As shown in below diagram.

I want to add one more element in multi-instance loop(now count is 5) and want to add variables(City and CityLocal) specific to that instance for business requirement.

I am able to achieve this using below java api but i can see variables scope is set at process and local level and not at multi-instance subflow level.

I am expecting scope should be like variable (itemId).

Can you suggest how to achieve this or whether its possible to achieve this?

runtimeService.createProcessInstanceModification(modProcId)
.startBeforeActivity(“orderItemloop”)
.setVariableLocal(“CityLocal”, “LondonLocal”)
.setVariableLocal(“AddressLocal”, “1234566Local”)
.setVariable(“City”, “London”)
.setVariable(“Address”, “1234566”)
.execute();

Please suggest how we can achieve this.

Thank you.

Hi @Pradip_Patil

[ACTIVITY_ID]#multiInstanceBody is a single outer scope for the created inner elements at which system variables nrOfActiveInstances, nrOfCompletedInstances, and nrOfInstances are automatically created. Variables specific to each instance/element of the collection are not supposed to be defined there as each instance has its own scope where those specific variables should reside.

Got your point that we should not update it at [ACTIVITY_ID]#multiInstanceBody and its purpose is different. And avoce seVariable is setting variable on the process which we are modifying. Is there any way to add/set variables if we add element/loop to multi-instance activity(subflow)?

I can add the element/loop using business process modification API start before activity method. But also want to pass some variables specific to that scope.

Hi @Pradip_Patil, I might be misunderstanding exactly what you are trying to achieve but have your tried removing the the setVariable calls in the modification and aligning the names of the setVariableLocal?

runtimeService.createProcessInstanceModification(modProcId)
.startBeforeActivity(“orderItemloop”)
.setVariableLocal(“City”, “LondonLocal”)
.setVariableLocal(“Address”, “1234566Local”)
.execute();

and putting an input mapping on your inner “Groovy Script” for Address and City.

My experiment below … adds a new item after 30 seconds using the same process modification approach:

add_item.bpmn (6.2 KB)

Hi @Pradip_Patil

SetVariableLocal should do the job
Kindly find attached a working example (2 processes)

test_multi process creates 3 elements

add_item4_process process has a script task to add the 4th element

var runtimeService = execution.getProcessEngineServices().getRuntimeService();

var processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey("B001").singleResult();
runtimeService.createProcessInstanceModification(processInstance.getId())
  .startBeforeActivity("Activity_03td3fn")
  .setVariableLocal("element", S("{\"id\": 4,\"name\": \"Item 4\",\"description\":\"Description of item 4\"}"))
  .execute();

The new element has been added to the correct scope

I have tried same earlier and now as well but i can see scope is blank in my case.

def modProcId = execution.getVariable(“modProcId”);

runtimeService.createProcessInstanceModification(modProcId)
.startBeforeActivity(“orderItemloop”)
.setVariableLocal(“country”, “France”)
.execute();

BPMN_GroovyScript.bpmn (5.4 KB)
BPMN_MULTIINST_MOD.bpmn (2.8 KB)

Hey @Pradip_Patil, strange, I am not sure why you are seeing a blank scope. I ran your two processes with the following (expected) results. I am using 7.22 community edition.

Your modification script:

In my case I can see below pop up/Notification appearing and then disappearing-

Could not load scope for variables of completed process instances: too many activity instances.

This could be the reason. On my local camunda process/activities are almost none. and process instances is in running state-

Hey @Pradip_Patil, I am not sure what is going on here.

The only other suggestion would be to use the API to get the executions in your process instance and the the local variables for each execution. I found it a bit cumbersome to do this but it might help you prove that the scope is correct (and there is some issue with Cockpit) or maybe a similar error message via the API that you are getting via Cockpit.

Otherwise, raise a ticket with support if you can.