Where can I use scripts?
- Script tasks (of course)
- Execution listener
- Task listener
- Condition of sequence flows
- Input output mapping (added in 7.2.0-alpha2)
Why should I use scripts?
Let there be scripts
<bpmn2:scriptTask id="scriptTask" name="Welcome Message" scriptFormat="groovy">
<bpmn2:extensionElements>
<camunda:inputOutput>
<camunda:inputParameter name="player">
<!-- script as input parameter -->
<camunda:script scriptFormat="groovy">
players = ["Fritz", "Heinz", "Horst", "Klaus"]
players[new Random().nextInt(players.size())]
</camunda:script>
</camunda:inputParameter>
</camunda:inputOutput>
</bpmn2:extensionElements>
<!-- script as script task -->
<bpmn2:script>
println "Hello $player! Welcome to our casino"
</bpmn2:script>
</bpmn2:scriptTask>
We use a input parameter mapping to randomly select our new player. Therefore a groovy script is evaluated and the result of the last script line is saved to the local variable player. Inside the script task than another groovy script just welcomes the newly select player with a kind message.
At the start of the exclusive gateway we roll the dice. To do so we use an execution listener for the start event of the gateway.
<bpmn2:exclusiveGateway id="gateway" name="Dice">
<bpmn2:extensionElements>
<camunda:executionListener event="start">
<!-- script as execution listener -->
<camunda:script scriptFormat="groovy">
aNumber = new Random().nextInt(6) + 1
println "$aNumber was diced"
execution.setVariable('aNumber', aNumber)
</camunda:script>
</camunda:executionListener>
</bpmn2:extensionElements>
</bpmn2:exclusiveGateway>
The execution listener is another script which saves the result as a process variable so it can be used to evaluate the following sequence flow conditions. The variable is set with the help of the execution variable which corresponds to the DelegateExecution interface.
The conditions of the sequence flow could be expression language but this is a blog post about scripting so lets use a script for that.
<bpmn2:sequenceFlow id="flow3" name="Greater than 3" sourceRef="gateway" targetRef="userTaskPlayerWins">
<!-- script as sequence flow condition -->
<bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression" language="groovy">
aNumber > 3
</bpmn2:conditionExpression>
</bpmn2:sequenceFlow>
<bpmn2:sequenceFlow id="flow4" name="Less then 4" sourceRef="gateway" targetRef="userTaskBankWins">
<!-- script as sequence flow condition -->
<bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression" language="groovy">
<![CDATA[
aNumber < 4
]]>
</bpmn2:conditionExpression>
</bpmn2:sequenceFlow>
Both groovy scripts just evaluate the process variable set by the exclusive gateway.
The last action is to inform the system which user task should be executed. For that we add a task listener to both user tasks for the create event.
<bpmn2:userTask id="userTaskBankWins" name="Bank Wins">
<bpmn2:extensionElements>
<camunda:taskListener event="create">
<!-- script as task listener -->
<camunda:script scriptFormat="groovy">
println "User task '$task.name' was $task.eventName"
</camunda:script>
</camunda:taskListener>
</bpmn2:extensionElements>
</bpmn2:userTask>
<bpmn2:userTask id="userTaskPlayerWins" name="Player Wins">
<bpmn2:extensionElements>
<camunda:taskListener event="create">
<!-- script as task listener -->
<camunda:script scriptFormat="groovy">
println "User task '$task.name' was $task.eventName"
</camunda:script>
</camunda:taskListener>
</bpmn2:extensionElements>
</bpmn2:userTask>
The task listeners are again groovy scripts which uses the task variable to print the task name and the event. The task variable corresponds to the DelegateTask interface.
So that’s it. A whole process without a single line of Java code. To see the complete BPMN xml have a look at this gist. Or if you want directly start playing with it you can clone a test project here.
Now it’s your turn. Have fun and start scripting. And if you have any remarks our if you miss a features please let us know.

Camunda Developer Community
Join Camunda’s global community of developers sharing code, advice, and meaningful experiences