Use of Transient Variable in JavaScript With Camunda External Task – DZone Web Dev

In this article, let’s see how to use transient variables while using javascript to implement Camunda external task.


  1. Camunda 7.17
  2. Eclipse with Maven (since Embedded Camunda Engine is used, any IDE)
  3. Java
  4. Visual Studio Code (Just to write and test the Javascript code; any other means also works)

Let’s begin

Follow the link to have an Embedded Camunda engine application if not done earlier. Let’s modify the process.BPMN to have an external implementation of the service task as below. The other script task is used to just print the value of the transient variable set by the JavaScript external worker.

Below code snippet can be used/ saved as BPMN to have the above implementation.

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="" xmlns:bpmndi="" xmlns:dc="" xmlns:di="" xmlns:xsi="" xmlns:camunda="" id="Definitions_0fr9mxs" targetNamespace="" exporter="Camunda Modeler" exporterVersion="5.1.0">
  <bpmn:process id="camunda-new-features-js-externalclient" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="Start">
    <bpmn:sequenceFlow id="Flow_1ql0hfr" sourceRef="StartEvent_1" targetRef="Activity_08xjz19" />
    <bpmn:endEvent id="Event_0ptn8u4" name="End">
        <camunda:executionListener event="end">
          <camunda:script scriptFormat="JavaScript">print("process executed.");</camunda:script>
    <bpmn:sequenceFlow id="Flow_0adr27u" sourceRef="Activity_08xjz19" targetRef="Activity_0zpmh2y" />
    <bpmn:serviceTask id="Activity_08xjz19" name="Call External Javascript Client" camunda:type="external" camunda:topic="javascript-client">
    <bpmn:sequenceFlow id="Flow_0beer3a" sourceRef="Activity_0zpmh2y" targetRef="Event_0ptn8u4" />
    <bpmn:scriptTask id="Activity_0zpmh2y" name="Print Transient Variable" scriptFormat="JavaScript">
      <bpmn:script>print("Transient Variable value: "+execution.getVariable("age"))</bpmn:script>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="camunda-new-features-js-externalclient">
      <bpmndi:BPMNEdge id="Flow_0adr27u_di" bpmnElement="Flow_0adr27u">
        <di:waypoint x="400" y="117" />
        <di:waypoint x="450" y="117" />
      <bpmndi:BPMNEdge id="Flow_1ql0hfr_di" bpmnElement="Flow_1ql0hfr">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="300" y="117" />
      <bpmndi:BPMNEdge id="Flow_0beer3a_di" bpmnElement="Flow_0beer3a">
        <di:waypoint x="550" y="117" />
        <di:waypoint x="622" y="117" />
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
          <dc:Bounds x="185" y="142" width="25" height="14" />
      <bpmndi:BPMNShape id="Activity_14doiiu_di" bpmnElement="Activity_08xjz19">
        <dc:Bounds x="300" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      <bpmndi:BPMNShape id="Event_0ptn8u4_di" bpmnElement="Event_0ptn8u4">
        <dc:Bounds x="622" y="99" width="36" height="36" />
          <dc:Bounds x="630" y="142" width="20" height="14" />
      <bpmndi:BPMNShape id="Activity_1grvs1w_di" bpmnElement="Activity_0zpmh2y">
        <dc:Bounds x="450" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />

Build and run the application.

Next, let’s build a JavaScript worker code which can set the transient variable above the process model.

Use the below code snippet to do the same and save it as a “.js” file. Here in the code snippet transient variable “age” is set to a value of 50.

const {Client, logger} = require("camunda-external-task-client-js");
const {Variables} = require("camunda-external-task-client-js");

const client = new Client ({baseUrl: "http://localhost:8082/engine-rest", use: logger});

client.subscribe("javascript-client", async function({ task, taskService }) {
  const variables = new Variables ();

  variables.setTransient("age", "50"); //Setting Transient variable for the process
  await taskService.complete(task, variables);

Execute the script. 

To get help in running JavaScript snippets follow the video tutorial courtesy Camunda Team.

Next, use Camunda cockpit to start the process instance and the result will be printed on the eclipse/IDE console as the Value of the transient variable as 50(set in the JavaScript code).

Let’s try and verify if this process variable is saved in the Camunda DB. Open H2 DB (used for this deployment).


Select all data from a table

The transient variable “age” is not saved in the DB whereas the same is accessible in the process and can be used in various use-cases in the process modelling.

Hope that Helps.

Quoted from Various Sources

Published for: The Bloggers Briefing