A Script Task is defined by specifying the script and the scriptFormat.

The value of the scriptFormat attribute must be a name that is compatible with JSR-223 (Scriptingfor the Java Platform). If you want to use a (JSR-223 compatible) scripting engine, you need to toadd the corresponding jar to the classpath and use the appropriate name.

The script source code has to be added as the text content of the child element.Alternatively, the source code can be specified as an expression or external resource. For moreinformation on the possible ways of providing the script source code please see the corresponding of the User Guide.

Camunda BPM should work with most of the JSR-223 compatible script engine implementations. We test integration for Groovy, JavaScript, JRuby and Jython. See the section of the User Guide for more details.

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 the example below, the script variable inputArray is in fact a process variable (an array of integers).

It’s also possible to set process variables in a script. Variables can be set by using the setVariable(…) methods provided by the VariableScope interface:

By setting the property autoStoreScriptVariables to true in the process engine configuration, the process engine will automatically store all global script variables as process variables. This was the default behavior in Camunda BPM 7.0 and 7.1 but it only reliably works for the Groovy scripting language (see the section of the Migration Guide for more information).

  • set autoStoreScriptVariables to true in the process engine configuration
  • prefix all script variables that should not be stored as script variables using the def keyword: def sum = 0. In this case the variable sum will not be stored as process variable.

The configuration flag is only supported for Groovy Script Tasks. If enabled for other script languages,it is not guaranteed which variables will be exported by the script engine. Forexample, Ruby will not export any of the script variables at all.

Note: the following names are reserved and cannot be used as variable names:out, out:print, lang:import, context, elcontext.

Script Results

The return value of a Script Task can be assigned to a previously existing or to a new process variable by specifying the process variable name as a literal value for the attribute of a Script Task definition. Any existing value for a specific process variable will be overwritten by the result value of the script execution. When a result variable name is not specified, the script result value gets ignored.

In the above example, the result of the script execution (the value of the resolved expression #{echo}) is set to the process variable named myVar after the script completes.

Camunda Extensions