How camunda uses script nodes

Posted by CoB-Himself on Mon, 13 Dec 2021 07:37:28 +0100

In camunda, the script task is an automatic activity. When the process executes the script task, the relevant script is automatically executed. Camunda supports mostly JSR-223 compatible script engine implementations, such as Groovy, JavaScript, JRuby and Jython.

This paper focuses on the description of the script engine based on JavaScript, configuring Camunda script service, completing the automatic calculation based on script, and returning the calculation results to the process variables.

1, Design flow chart

 

The following are key configuration items for script nodes:

 

There are two ways to set the return value of script calculation results:

First, it is set through the result Variable attribute. By specifying the process variable name as the text value of the camunda:resultVariable property of the script task definition, you can assign the return value of the script task to a pre-existing process variable or a new process variable. Any existing value of a specific process variable will be overwritten by the result value of the script execution. When no result Variable name is specified, the script result value is ignored.

Second, through execution Setvariable ("variable name", "variable value"); You can set multiple variables in the script, which is more flexible.

The following is the complete BPMN model file:

<?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:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0ppniex" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
  <bpmn:process id="Process_0a6gw7u" name="Loan application process Script" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>Flow_0kzdck2</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_0kzdck2" sourceRef="StartEvent_1" targetRef="Activity_1l6o8wm" />
    <bpmn:scriptTask id="Activity_1l6o8wm" name="Calculate loan limit" scriptFormat="JavaScript">
      <bpmn:incoming>Flow_0kzdck2</bpmn:incoming>
      <bpmn:outgoing>Flow_0h8bikl</bpmn:outgoing>
      <bpmn:script>var yearWages = execution.getVariable("yearWages");
var houseAssets = execution.getVariable("houseAssets");
execution.setVariable("loanLimit", (yearWages+houseAssets)*0.8);</bpmn:script>
    </bpmn:scriptTask>
    <bpmn:sequenceFlow id="Flow_0h8bikl" sourceRef="Activity_1l6o8wm" targetRef="Activity_1wjgiji" />
    <bpmn:userTask id="Activity_1wjgiji" name="Confirm loan limit" camunda:assignee="demo">
      <bpmn:incoming>Flow_0h8bikl</bpmn:incoming>
      <bpmn:outgoing>Flow_03h3srs</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="Flow_03h3srs" sourceRef="Activity_1wjgiji" targetRef="Event_0myx83u" />
    <bpmn:endEvent id="Event_0myx83u">
      <bpmn:incoming>Flow_03h3srs</bpmn:incoming>
    </bpmn:endEvent>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0a6gw7u">
      <bpmndi:BPMNEdge id="Flow_03h3srs_di" bpmnElement="Flow_03h3srs">
        <di:waypoint x="600" y="117" />
        <di:waypoint x="692" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0h8bikl_di" bpmnElement="Flow_0h8bikl">
        <di:waypoint x="390" y="117" />
        <di:waypoint x="500" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0kzdck2_di" bpmnElement="Flow_0kzdck2">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="290" y="117" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0myx83u_di" bpmnElement="Event_0myx83u">
        <dc:Bounds x="692" y="99" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_187in45_di" bpmnElement="Activity_1wjgiji">
        <dc:Bounds x="500" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0w973ww_di" bpmnElement="Activity_1l6o8wm">
        <dc:Bounds x="290" y="77" width="100" height="80" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

2, Initiate process test

Sign in: http://localhost:8080/camunda/app/admin/default/#/login

1. Initiate a process and enter process variables. These two process variables are required for subsequent script node calculations

 

2. After submitting the process, view the flowchart and the Script node has been successfully executed

 

3. View the process variables in the form. The calculated return value of the Script node has been written successfully.

 

More references:

https://docs.camunda.org/manual/7.15/reference/bpmn20/tasks/script-task/

https://github.com/camunda/camunda-bpm-examples/tree/7.15/scripttask

Cloud process | cloud BPM, cloud process BPM, low code platform, low code development platform, open source process engine, Camunda,flowable, business process management, activiti, intelligent form, electronic form, visual development, zero code development, basic platform, process PaaS, process SaaS

Topics: Javascript Activiti camunda