Script Task
Overview
A script task is a task that executes a Groovy script when it is reached in the process flow.
Commonly, script tasks are used when we need to initialize process variables, set default values, evaluate expressions, and do some calculations — everything that can be done with a small piece of code.
But not limited to the above. You can implement any business logic inside your script, it’s up to you.
Jmix BPM supports only Groovy scripts. But the original Flowable product allows JavaScript as well. Be attentive when importing process models from external sources. |
A script task is visualized as a typical BPMN 2.0 task (rounded rectangle), with a small 'script' icon in the top-left corner of the rectangle.
A script task is defined by specifying the script
and the scriptFormat
.
<scriptTask id="script-task" name="Script task"
scriptFormat="groovy" (1)
flowable:resultVariable="result"> (2)
<script>
printlnt "This is a script" (3)
return "OK"
</script>
</scriptTask>
1 | — Script type (only Groovy) |
2 | — Result variable (optional) |
3 | — Script body |
Properties
Script task has the following specific properties:
-
scriptFormat: an extended attribute that indicates the language of the script (only Groovy).
-
script: the script to execute, defined as a string in a field element named "script".
-
resultVariable: optional task attribute that when present will store a variable with the specified name in the execution context with the script evaluation result (see note below).
To edit the script, use Script editor by clicking twice on the script field:
The script body cannot be empty, otherwise it causes a parsing error during deployment. |
Variables in Scripts
All process variables that are accessible through the execution that arrives in the script task can be used within the script. In this example, the script variable 'inputArray' is in fact a process variable (array of integers).
<script>
sum = 0
for ( i in inputArray ) {
sum += i
}
</script>
It’s also possible to set process variables in a script, simply by calling
execution.setVariable("variableName", variableValue)
By default, no variables are stored automatically.
Script Result
The return value of a script task can be assigned to an existing process variable or a new one. To specify the target variable, you can set it in the Result variable field in the properties panel of the script task.
When a script task is executed, the script’s return value will be automatically assigned to the specified process variable. If the variable doesn’t exist, Flowable will create a new one with the same name as the Result variable field.
By leveraging the Result variable field, you can integrate the results of script tasks into your process variables, enabling you to store, manipulate, and access data throughout the process execution.
The return value of a script can be assigned to an existing variable or to a new process variable. You can set it in the Result variable field in the properties panel.
In the script, you can use the 'return' operator. For example:
def a = "abc"
return a
If there is no 'return' in your script, as a result will be taken result of the last line, in this example it’d be "JMIX".
String a = "abc".toUpperCase()
String b = "jmix".toUpperCase()