Skip to content

Scripting

Script Task

JavaScript scripting in a Workflow can be performed by using a script task. When the process execution arrives at the script task, the attached script is executed. To use JavaScript, the Script Format in the script task can be specified to be JavaScript.

ScriptTask

Below is the generated XML for the script task.

<bpmn:scriptTask id="ScriptTask\_19c2f27" name="Inc Counter"  scriptFormat="JavaScript">  
      <bpmn:incoming>SequenceFlow\_19jwfjw</bpmn:incoming>  
      <bpmn:outgoing>SequenceFlow\_1h10vij</bpmn:outgoing>  
      <bpmn:script>var counter = execution.getVariable('counter\_t');  
        execution.setVariable('counter\_t',counter + 1);  </bpmn:script>  
 </bpmn:scriptTask>

Refer Camunda Documentation link for more information.

Numeric Functions

Numeric functions can be performed using JavaScript in a Script Task. Some common arithmetic functionality includes:

  • Sum
  • Subtract
  • Multiply
  • Divide

As shown in the figure below, arithmetic can be used on different numeric variables, and the results set in the execution variables to be used in another process state.

NumericFunction

Date Functions

Dates can be created and manipulated with JavaScript.

The Date object contains information about the:

  • Day
  • Month
  • Year
  • Time

Dates can be manipulated using built-in functionality in the Date object. In the figure below, days, months, and years can be added or subtracted to the current date. Time can also be added or subtracted by seconds, minutes, and hours using the built-in functionality.

Refer to the JavaScript documentation for Date functionality here.

DateFunction

Strings

Strings can be built and manipulated using JavaScript. Common string manipulation such as concatenation and substring can be performed.

To concatenate a string, the ‘+’ can be used to concatenate multiple strings and variables together. As shown in the figure below, the string concatenates two variables retrieved from the execution.

Substring functionality is a native method provided in JavaScript. The substring() method will return the string between the start and end indexes. Refer to the JavaScript documentation for the substring method here.

Strings