Unable to use execution.setVariable() in listener inline script

Goal

I need to set/update several process instance variables based on some task form variables.

Idea

I thought of using a Script Task Listener on the corresponding User Task to code the logic directly into my BPMN file. I don’t want to code anything in Java since our whole application is written in PHP and we only interface with camunda process-engine through its REST API.

Attempt

Below is the Task Listener definition I used on the User Task for testing purposes:

Event Type : Complete
Listener Type: Script
Script Format: Javascript
Script Type: Inline Script
Script: execution.setVariable(“test”, “foo”);

I completed the task using Camunda REST API. I’m using Camunda 7.8.0 full distro (camunda-bpm-tomcat-7.8.zip) on Windows for testing.

Outcome

I receive the following error message:

HTTP: 500 Internal Server Error
{
“type”: “RestException”,
“message”: “Cannot complete task 6ef81d60-5ad8-11e8-ab04-34e6d7457596: ENGINE-03051 There was an exception while invoking the TaskListener. Message: ‘Unable to evaluate script: ReferenceError: “execution” is not defined in at line number 1’”
}

Here is the corresponding Exception Stack Trace.txt (18.3 KB) from camunda.

Surprizing Facts

  1. I can successfully use an Expression Task Listener on the User Task with the expression set to “${execution.setVariable(‘test’, ‘foo’)}”.
  2. I can also successfully use a Script Task with an inline Javascript set to “execution.setVariable(“test”, “foo”);”.

Questions

  1. What did I missed? What is so special about User Task Inline Script Listener?
  2. How to set/update process instance variables in an User Task Inline Script Listener script?
  3. Are they better ways to reach the goal? If so, what are they?

Thanks in advance.

1 Like

Hi @pmessier ,

Inside the script of task listener, the variable task is available, which corresponds to the DelegateTask interface.

You could get the execution instance as below
task.execution

Your code should be
task.execution.setVariable(“test”, “foo”);

See below link
https://docs.camunda.org/manual/7.7/user-guide/process-engine/scripting/#use-scripts-as-task-listeners

3 Likes

Thanks Hassan for the pointer. For some reason, I missed that keyword when I scanned the doc.

Hi @hassang ,

Can you help me too?
I’d like to use the same script on Sequence flow for declaring variable with some value.

For example:
task.execution.setVariable(“flag”, “true”);

My main idea is Declaring any variables with values on different Sequence flows and use this variables in next tasks.

How can I do it?

Thanks.

Hi @td_gokhra
Execution listener (Not task listener) is used to execute custom code (In your case: define a new variable) when the transition “sequence flow” is taken.
Inside the script of execution listener, the variable execution is available, which corresponds to the DelegateExecution interface.

Which means
execution.setVariable(“flag”, “true”);
Should be used

For more information about execution listener, see below link

https://docs.camunda.org/manual/7.8/user-guide/process-engine/delegation-code/#execution-listener

1 Like

Hi @hassang, I am trying to update different values to single variable in various sequence flow. But the variable is not getting updated.

<bpmn:sequenceFlow id="SequenceFlow_0097vsx" name="Submit" sourceRef="ExclusiveGateway_010dnct" targetRef="Task_1wo8m1b">
  <bpmn:extensionElements>
    <camunda:executionListener expression="${execution.setVariable('workflowStatus', 'New Request')}" event="take" />
  </bpmn:extensionElements>
  <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${isSubmitted== 'Yes'}</bpmn:conditionExpression>
</bpmn:sequenceFlow>