setVariable() using value parameter of another process variable name (javascript)

@camunda can someone explain this system behaviour (and assuming this is not a bug, the reasoning/use case).

Consider the following:

“create variable”: contains the following javascript:

execution.setVariable("myTestVar", "value123");

“Create second variable”: contains the following javascript:

execution.setVariable("myTestVar2", myTestVar);

If you run this, no errors are thrown and myTestVar2 is created with the process variable value of myTestVar. Why is the myTestVar value available to the second task when it is placed as the value parameter of the setVariable() method?

If the second task’s script was just:

myTestVar;

the script would error out and not be able to find the variable.

Is this the expected behaviour? if so, why?

Thanks!

1 Like

Hi @StephenOTT,

I think this happens due to the definition of “scope”. Variable scope and scope available to javascript engine are not equal. When you use setter method, variable substitution will take place and value will be used as a reference to set real value of myTestVar2 into process instance variable scope. While javascript is just not aware of such mechanic.

Does that make sense to you?
Askar

Is this a expected /planned behaviour? Or just a result of the script engine’s functionality? If it was expected, is there a specific use case for such functionality?

For me making variables provided or copied into JS scope during execution would be a feature request. If it was explicitly planned like this is out of my knowledge.

Cheers,
Askar