Add local variable to service task (delegate expression)

Hello everyone,

I am confused about the execution / variable scope system. I want to use an expression to add a local variable in an execution listener that is added to a service task in camunda modeler.

${execution.setVariableLocal('variableName','value')}

As far as I understand, a service task (as a delegate expression) does not create an execution on its own. But I have noticed, that a service task (as external task) does have an own execution and it is possible to add a local variable. The same applies to Human Task, but there you have to use the following expression:

${task.setVariableLocal('variableName','value')}

Q: Is there a way to add a local variable to a service task (as a delegate expression)?

There are a couple of posts which try to clearify the execution system but I could really find a good explaination:

[1] java - Camunda BPM execution and variable scope misunderstanding - Stack Overflow
[2] Process Variables | docs.camunda.org
[3] PVM Execution Tree · camunda/camunda-bpm-platform Wiki · GitHub

Hi @Jonathan_H ,

you can set those variables up through Java API (that is if you are using Camunda Embedded edition). For exmaple this method might be useful

setVariableLocal(String taskId, String variableName, Object value)
set variable on a task.

Please refer to the TaskService documentation here

You can also set them up through REST. Please have a look here.

Also please keep in mind that the setVariableLocal() methods are especially of interest when you want to limit the visibility of variables, for example in cases where there is a parallel gateway and you want some variables to be only visible to one branch of execution but not the other.

Maybe you will find this thread useful as well.

Hello @Michal_Stefaniuk ,
thanks for you response. In the google thread that you linked it states:

Here, “task instance” means an instance of org.camunda.bpm.engine.task.Task, not the process model element that is also called “task” or “userTask” or “serviceTask” etc. There is no concept of local variables of a “serviceTask”.

Do I understand it correctly, that a “task instance” could be of type serviceTask or userTask and does not stand for userTask exclusively? Because that is not my experience. As far as I noticed, camunda is not creating a “task instance” for a serviceTask, as Illustration 1 showes. Execution_Scope_1 is the process execution. Execution_Scope_1 is the addtional execution created by the UserTask.
image
Illustration 1: Execution Scopes as I think it is.

What I would like to execute execution.setVariableLocal on the ServiceTask, so that the variable disapears afterwards. The only workaround I found so far is to add two listener to the ServiceTask, one on start event, to add the variable, and another one on end event, to delete the variable from Execution_Scope_1.

Q: Have I observed this correctly or do I still have an understanding error here?

1 Like

Hello @Jonathan_H ,

yes, I think you have a good understanding of the flow. If it still raises your concerns, feel free to share your code base/process/link to github repo with a proof of concept of this solution - maybe this would make it easier for us to discuss it further :slight_smile:

1 Like