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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Comparison and Usage of Javascript Engines in Camunda
  • JS (React) Technical Interview: Structure, Coding Tasks Examples, Tips
  • A Camunda Greenhouse: Part 3
  • Top Seven JavaScript Kanban Widgets for Effective Task Management

Trending

  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Driving DevOps With Smart, Scalable Testing
  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.

By 
Alok Singh user avatar
Alok Singh
DZone Core CORE ·
Sep. 26, 22 · Analysis
Likes (1)
Comment
Save
Tweet
Share
4.3K 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.

Related

  • Comparison and Usage of Javascript Engines in Camunda
  • JS (React) Technical Interview: Structure, Coding Tasks Examples, Tips
  • A Camunda Greenhouse: Part 3
  • Top Seven JavaScript Kanban Widgets for Effective Task Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!