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

**URL:** <https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372>\
**Category:** Discussion & Questions\
**Tags:** process-engine\
**Created:** [May 18, 2018, 8:48pm UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372 "2018-05-18T20:48:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmessier](https://forum.camunda.io/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@pmessier](https://forum.camunda.io/u/pmessier)\
**Post date:** [May 18, 2018, 8:48pm UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/1 "2018-05-18T20:48:00Z")

</div>

**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](https://forum.camunda.io/uploads/default/original/2X/7/722588cd6d261c4a1c5e4fa4d6362e5ae172613c.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.

---

<div class="post-metadata">

**Author:** ![hassang](https://forum.camunda.io/user_avatar/forum.camunda.io/hassang/32/16498_2.png) [@hassang](https://forum.camunda.io/u/hassang)\
**Post date:** [May 19, 2018, 10:32am UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/2 "2018-05-19T10:32:41Z")

</div>

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](https://docs.camunda.org/manual/7.7/user-guide/process-engine/scripting/#use-scripts-as-task-listeners)

---

<div class="post-metadata">

**Author:** ![pmessier](https://forum.camunda.io/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@pmessier](https://forum.camunda.io/u/pmessier)\
**Post date:** [May 22, 2018, 1:09pm UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/3 "2018-05-22T13:09:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![td\_gokhra](https://forum.camunda.io/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@td\_gokhra](https://forum.camunda.io/u/td_gokhra)\
**Post date:** [May 23, 2018, 8:33am UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/4 "2018-05-23T08:33:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hassang](https://forum.camunda.io/user_avatar/forum.camunda.io/hassang/32/16498_2.png) [@hassang](https://forum.camunda.io/u/hassang)\
**Post date:** [May 23, 2018, 9:19am UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/5 "2018-05-23T09:19:04Z")

</div>

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](https://docs.camunda.org/manual/7.8/user-guide/process-engine/delegation-code/#execution-listener)

---

<div class="post-metadata">

**Author:** ![ChandruM](https://forum.camunda.io/user_avatar/forum.camunda.io/chandrum/32/7185_2.png) [@ChandruM](https://forum.camunda.io/u/ChandruM)\
**Post date:** [June 23, 2020, 1:47pm UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/6 "2020-06-23T13:47:29Z")

</div>

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>
```

---

<div class="post-metadata">

**Author:** ![Yurii\_Shchur](https://forum.camunda.io/user_avatar/forum.camunda.io/yurii_shchur/32/13011_2.png) [@Yurii\_Shchur](https://forum.camunda.io/u/Yurii_Shchur)\
**Post date:** [March 11, 2021, 9:32am UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/7 "2021-03-11T09:32:11Z")

</div>

Hi @ChandruM.  
You probably don’t need this answer anymore but maybe it will be helpful for people who see this question. As far as I know, you can’t write such things as `execution.setVariable('workflowStatus', 'New Request')` inside expression. Try to use Script Listener type. If you want to write groovy inline script, for example, then it will look the same as the code inside your expression.

---

<div class="post-metadata">

**Author:** ![Damir\_Kovacevic](https://forum.camunda.io/user_avatar/forum.camunda.io/damir_kovacevic/32/14508_2.png) [@Damir\_Kovacevic](https://forum.camunda.io/u/Damir_Kovacevic)\
**Post date:** [May 27, 2021, 12:20pm UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/9 "2021-05-27T12:20:27Z")

</div>

Sorry, I am new into Camunda Forum.  
I will try aggain.

I successfully use scrip task in Camunda-modeler-4.8.1 and Camunda 7.15

I seutp script task like in picture:

 ![Camunda-modeler-4.8.1-win-x64- script task - inline script set process variable Camunda 7.15](https://forum.camunda.io/uploads/default/original/2X/8/89ae34f2c378f2c52683cdc1a0db20d97f77b7f0.png)

Start process and get new process variables:

 ![Camunda-cockpit- script task - inline script set process variable Camunda 7.15](https://forum.camunda.io/uploads/default/original/2X/e/e83fe715ebcedeae480d664ba290013949496e78.png)

🙂 Chears

---

<div class="post-metadata">

**Author:** ![system](https://forum.camunda.io/uploads/default/original/3X/5/8/58d50142eacca605d35cb1ef9e6b86ae85f49436.png) [@system](https://forum.camunda.io/u/system)\
**Post date:** [January 30, 2024, 11:13am UTC](https://forum.camunda.io/t/unable-to-use-execution-setvariable-in-listener-inline-script/7372/10 "2024-01-30T11:13:54Z")

</div>

Closed on January 30, 2024, 11:13am UTC


