DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. Use of Transient Variable in JavaScript With Camunda External Task

Use of Transient Variable in JavaScript With Camunda External Task

Learn how to use transient variables while using JavaScript to implement Camunda external task.

Alok Singh user avatar by
Alok Singh
CORE ·
Sep. 26, 22 · Analysis
Like (1)
Save
Tweet
Share
3.31K Views

Join the DZone community and get the full member experience.

Join For Free

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

Pre-requisite:

  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.

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
 
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_0fr9mxs" targetNamespace="http://bpmn.io/schema/bpmn" 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:outgoing>Flow_1ql0hfr</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_1ql0hfr" sourceRef="StartEvent_1" targetRef="Activity_08xjz19" />
    <bpmn:endEvent id="Event_0ptn8u4" name="End">
      <bpmn:extensionElements>
        <camunda:executionListener event="end">
          <camunda:script scriptFormat="JavaScript">print("process executed.");</camunda:script>
        </camunda:executionListener>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_0beer3a</bpmn:incoming>
    </bpmn:endEvent>
    <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:incoming>Flow_1ql0hfr</bpmn:incoming>
      <bpmn:outgoing>Flow_0adr27u</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow_0beer3a" sourceRef="Activity_0zpmh2y" targetRef="Event_0ptn8u4" />
    <bpmn:scriptTask id="Activity_0zpmh2y" name="Print Transient Variable" scriptFormat="JavaScript">
      <bpmn:incoming>Flow_0adr27u</bpmn:incoming>
      <bpmn:outgoing>Flow_0beer3a</bpmn:outgoing>
      <bpmn:script>print("Transient Variable value: "+execution.getVariable("age"))</bpmn:script>
    </bpmn:scriptTask>
  </bpmn:process>
  <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>
      <bpmndi:BPMNEdge id="Flow_1ql0hfr_di" bpmnElement="Flow_1ql0hfr">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="300" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0beer3a_di" bpmnElement="Flow_0beer3a">
        <di:waypoint x="550" y="117" />
        <di:waypoint x="622" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="185" y="142" width="25" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_14doiiu_di" bpmnElement="Activity_08xjz19">
        <dc:Bounds x="300" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0ptn8u4_di" bpmnElement="Event_0ptn8u4">
        <dc:Bounds x="622" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="630" y="142" width="20" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_1grvs1w_di" bpmnElement="Activity_0zpmh2y">
        <dc:Bounds x="450" y="77" width="100" height="80" />
        <bpmndi:BPMNLabel />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

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.

JavaScript
 
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 * FROM ACT_HI_VARINST

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.

Camunda JavaScript Task (computing) Transient (computer programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Front-End Troubleshooting Using OpenTelemetry
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Solving the Kubernetes Security Puzzle
  • Custom Validators in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: