HI ,
i have a flow diagram as given below, wherein there is a parallel gateway which spawns 2 flows which go into a service task which should populate a variable “errorCode” and a value to it and then goes to a user task. now both the flows should have a variable “errorCode” with different values, but it is getting overridden.
i am populating the variable and value using the service task and in that using the following piece of code:
execution.setVariable(key, value);
If you set a variable through the execution.setVariable(key, value);
method it is set global across the process. so if you do it twice for the same variable it will be over-ridden. You can try execution.setVariableLocal(key, value);
this would create a local variable but might not work without a sub process around the tasks. It might be better to create different variable names for the error messages.
Hi @shashank19aug,
as you already mentioned, the variable gets overridden. If you check the implementation of variables, you’ll find it revolves around Java’s Map. Due to the very nature of maps, inserting a key twice will result in overriding the initial insertion value.
I suggest using a different approach, because both service tasks operate in the scope of the process thus overriding each other (based on how the parallel execution figures out which task fires first).
Cheers
Sascha
Hi @sdibernardo,
thanks
i understand the map part and why it gets overridden but is there a way to do something to achieve what my goal.
Hi @shashank19aug,
maybe this chapter of the docs helps you further: https://docs.camunda.org/manual/7.14/user-guide/process-engine/variables/#variable-scopes-and-variable-visibility
Hope this helps, Ingo
To further elaborate on @Ingo_Richtsmeier’s hint, you should introduce subprocesses and then use local variable in said subprocees order to use the same variable without overriding it.