User task: set variables in subprocess

Hi!

We are using camunda with spring boot. As a frontend, we use Angular (2+) with our own REST endpoint. For user tasks, there exists an endpoint to submit form data on basis of the taskId.

We found a strange behavior, when having multi-instance sub process tasks.
Our example:

When we submit the form in “Task A”, we resolve the task by

Task task = taskService.createTaskQuery()
      .active()
      .taskId(taskId)
      .singleResult();

and then call

formService.submitTaskForm(task.getId(), variables);

this works as expected. For “Task B”, the submitted variables have the wrong scope - the overall all execution - and not the one from the sub process instance. We want “Task C” to see the submitted value, but not “Task D”. Then, we thought of submitting no values but setting the variables on our own like this:

runtimeService.setVariablesLocal(task.getExecutionId(), variables);        
formService.submitTaskForm(task.getId(), new HashMap<>());

When using the execution ID attached to the task, we still got the wrong scope.

We looked at some tables in the Camunda DB and saw, that the “loopCounter” variable had the correct execution ID set. Therefore, our solution is the following

Object loopCounter= this.runtimeService.getVariable(task.getExecutionId(), "loopCounter");
if(loopCounter != null) {
  VariableInstance singleResult = runtimeService.createVariableInstanceQuery()
            .processInstanceIdIn(task.getProcessInstanceId())
            .variableValueEquals("loopCounter", loopCounter)
            .singleResult();
  runtimeService.setVariablesLocal(singleResult.getExecutionId(), variables);
}

If the loop counter is null, we know that we are not in a sub process and the standard submitTaskForm call works.

My question is:
Is this really the easiest way to do this? Did we get something wrong?

With a Java delegate, we found out that we can use this.

runtimeService.setVariableLocal(execution.getParentId(),variable);

best regards,
ML