Configure Script tasks

Script task Script task

Executes a script to directly access Java beans in the configuration as well as task and execution beans. Script tasks achieve most of the same functions of a Java class.

Examples

  • Initialize variables at the start of a process.

    Tip: Use a script task immediately after the start event to initialize variable values or retrieve data from a web service.

  • Parse form fields.
  • Access exposed spring bean properties.
  • Make decisions based on simple logic.
  • Transform data.

Languages for script tasks

Przestroga :Scripts are a powerful way to accomplish tasks or they can do a lot of damage. You must be qualified in the script language before you use a script task.

  • Groovy is strongly recommended because of fast performance, and it includes JsonSlurper to parse JSON responses from APIs.

  • JavaScript is supported but it suffers from slow performance, is less flexible, and makes it hard to access Java functions.

  • Any JSR-223 compatible language can be used. Java Specification Requests (JSR-223) defines a framework for embedding scripts into Java source code.

A script task can declare process variables: execution.setVariable("varDays",noOfDays+1);

To refer to a process variable or other expression from a text field, use the following notation: ${varName}

Uwaga: If the script generates a runtime exception, an error message is displayed. (R6?)

Error handling in script tasks

To minimize the chance of breaking a process due to an error, you can use a boundary event to move the flow to another route.

Screenshot showing boundary event in Process Model designer

For example,

  • try{
    • undefin();
  • }catch(error){
    • execution.setVariable("error_message", err.message);
    • throw new org.activiti.engine.delegate.BpmnError ("error_message",erromessage)'
  • }

Example script tasks