How to add new contributor in an active multi user task

Hi, I have a requirement where I want to add a new assignee to an active multiuser task.

Suppose, below is the definition of the task :
< userTask id=“myTask” name=“my task” flowable:assignee="${parallelContributor}">
< multiInstanceLoopCharacteristics isSequential=“true” flowable:collection=“parallelContributors” flowable:elementVariable=“parallelContributor”>
< /userTask>
while launching, “parallelContributors” variable was initialized with “user1”," user2".

Now after task is initiated, I want add one more user to this task. I am trying to do this using following code:

List<String> userList = new ArrayList<String>();
userList.add("user1");
userList.add("user2");
userList.add("user3");

Map<String, Object> newUserListVariables = new HashMap<String, Object>();
		variables.put("parallelContributors", userList);
List executions = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list();
for (Execution execution : executions) {
	if (execution instanceof ExecutionEntity) {
		ExecutionEntity executionEntity = (ExecutionEntity) execution;
		if (executionEntity.isMultiInstanceRoot()) {
			runtimeService.setVariables(execution.getId(), newUserListVariables);
			int nrOfInstances = (Integer) (runtimeService.getVariable(execution.getId(), “nrOfInstances”));
			runtimeService.setVariable(execution.getId(), “nrOfInstances”, userList.size());

Here, using setVariables method I updated the collection and then incremented “nrOfInstances” variable to match the latest count of users.

This is working for sequential type of task. But for parallel type of task, the execution for new user3 does not get initiated.
My question is, is this the correct way to add contributor, or is there some other way to add new contributor.
I am on version 6.3.1 of flowable.

Hey @khushboo,

I am not sure that I am following. Do you want to change the assignee of a currently running task or do you want to add a new task into the multi instance execution?

If you are trying to change the assignee I would suggest that you have a look at TaskService#setAssignee(String, String). I would advise you to have a look at TaskService#claim and TaskService#unclaim as well.

Cheers,
Filip

Something that I forgot to add. In case you want to add a completely new instance to the multi instance execution there is a better way to do it (only possible on v6 though).

You can add a new execution to an already running multi instance by calling RuntimeService#addMultiInstanceExecution(String, String, Map<String, Object>.

You can have a look at the DynamicMultiInstanceTest to see how it can be used.

Cheers,
Filip

Thanks Filip, sorry for late reply. Got some other priority item so could not verify the solution suggested by you. I checked DynamicMultiInstanceTest, but unfortunately I could not solve my problem with the solution suggested there.

Here is my problem scenario:
< userTask id=“myTask” name=“my task” flowable:assignee="${sequentialContributor}">
< multiInstanceLoopCharacteristics isSequential=“true” flowable:collection=“sequentialContributors” flowable:elementVariable=“sequentialContributor”>
< /userTask>

Suppose the task is simultaneous, and “sequentialContributors” variable was initialized with “user1”," user2" and “user3” users when workflow begins.
The “user1” is currently active. Now, I want to skip “user2” and want “user3” to act after “user1” completes.
I cannot use RuntimeService#deleteMultiInstanceExecution(String executionId, boolean executionIsCompleted), because task for “user2” is not yet created, so I don’t have executionId for it.

In all, I want to dynamically add and remove the users from the flowable:collection.

Did anybody has any ideas on how to “dynamically add and remove the users from the flowable:collection” ?