Task Local Variable created on a wrong scope

We have a process model which has a Multi instance sub process. Within the multi instance sub process we have a task loop (say a, b, c) The loop is executed as long as the conditions are satisfied. If the condition fails the loop is completed and the sub process instance. For each task a, b c, at the start of the task we execute listener which creates a local variables. The problem is that the task variable is created on the sub process scope rather than the actual task. Because of this, during the task loop execution, the existing task variable is updated, rather than creating a new task local variable for the task.

execution.setLocalVariable("test","test");

Please check the below bpmn diagram. How can we achieve creating the variable on the actual task scope rather than the sub process scope?

Hi @ChandruM, we had a similar issue once. It sets the variable on the scope of the multi-instance activity itself, which effectively overwrites the value. Especially with parallel executions this can cause problems.

The solution we took here is to actively create the local variable instances immediately when starting each instance. For this, we configured a (javascript) execution listener that simply does execution.setVariableLocal (with either an empty or a default or specific value (if we know it upfront)) for each instance. If the variable is actually already there, it will update the local one instead of on the parent scope once you update it in each instance.

Hi @tiesebarrell, Thanks for your response.

I did tried the approach you mentioned, but it did not help. Every time a new task is created in the subprocess, new set of variables to be created on the scope of the task. I do not want it to override the existing value of the variable.

This is a sample of how we configured this. It initialises the variables needed on the start event of each instance in the local scope. No parent scope variables are overwritten.

1 Like

Have you tried using a Task Listener rather than an Execution Listener?
As per the Variables Documentation, the local variable should only be visible to that specific task entity.

Best,
Tobias

Indeed, I misread that it needed to be local to the task :slight_smile: TaskListener is the solution in that case.

My suggestion was for the entire instance of the subprocess, then it makes sense to use an execution listener on the start event.

1 Like

Tasklistener does set the variable local to the task. The problem that i am facing is that i am not able to evaluate a condition in the gateway using the task local variable. Is there way where i can evaluate the condition using the task local varirable?

Ah, no. The task local variable is lost after the task is completed. In that case, you need my approach, which sets a variable to the local scope of the instance of the subprocess, making it available during the entire execution of that subprocess, in the task and the gateway alike.

Hmmm. I was able to set local variables for each instance of the subprocess in a multi instance subprocess. But the problem is that within the sub process i want to set task local variable and use that for gateway conditioning. The tasklistener did solve the problem of local variable. But then…

Why do you want it to be a task local variable, if it’s used in the gateway?

The entire scenario does not really make sense, as you wish to use a local variable outside of its intended scope. So now have two choices: omit the usage of local variables, so you can use them in your conditions once the task is completed or keep using them and utilize the output mapping of your task to create a new variable with global scope for subsequent usage of conditional expressions.

The idea is to preserve the values captured on a task, so that even during the cyclic flow, the values are captured freshly for each task.

Then you need the combination of a variable that is locally scoped to the subprocess, and those that are local to the task. Upon leaving the task, as @sdibernardo suggests, you need to “copy” the value of the task local variable into the scope of the subprocess in the output mappings of the task To do that effectively, you need to make sure the variable in the subprocess is actively initialised in that scope with a listener when the process is started (or at least before you need to update its value).

It sounds rather contrived to me too, but obviously we can’t oversee the details of your requirements. In many cases, the simpler solution with variables at the most global scope you can get away with tends to be the best option. That’s probably why the default implementation sets variables on the parent scope as well.

1 Like

Agreed. I too personally feel that it is adding more overhead. Thank you and everyone for helping me on this. Much appreciated.

1 Like