Skip to main content

Scripting

Introduction

Scripting in Flowable allows you to execute logic programmatically using low-code capabilities. A script consists of a set of instructions that are executed during runtime.

This guide demonstrates advanced scripting possibilities for performing various operations.

Scripts within Case and Process Models

You can use scripts directly in case and process models by using the Script Task to execute scripting logic. This method offers a powerful and efficient way to execute programmatic operations.

Within the script context, the entire application context is available, enabling a wide range of operations. You can leverage various Flowable APIs such as runtimeService, historyService, cmmnRuntimeService and more.

To easily access API methods, you can utilize the Flowable Scripting API (flw). Refer to Backend Scripting for more information.

To add a script, configure it directly within the Script Task of your BPMN and CMMN models.

Below is a basic example that demonstrates adding two numbers (case or process variables), performing an addition operation, and storing the result as a JSON object in the process.

// Get variables from Variable Container
var a = flw.getInput("firstNumber");
var b = flw.getInput("secondNumber");

// Perform operation
var c = a + b;

// Creation of the JSON object
var jsonObject = flw.json.createObject();
jsonObject.putInteger('firstNumber', a);
jsonObject.putInteger('secondNumber', b);
jsonObject.putString('operation', 'addition');
jsonObject.putInteger('result', c);

// Set variable to the Variable Container
flw.setOutput("result", jsonObject);

Scripts with the Service Registry

For creating reusable and abstracted scripts, you can utilize the Service Registry engine by configuring a Service Registry Model with a Service Type of Script.

You can add the same operation mentioned above, "addition", to a Service Registry Model by defining an operation called addition:

Service Registry Scripting Operation

Service Registry Scripting Operation

Service Registry Scripting Operation

Service Registry Scripting Operation

Note that the script logic remains identical to the script provided earlier. However, you need to explicitly set the input and output parameters as part of the Service Registry operation.

Once configured, you can use the Service Registry Model with the Service Registry Task in your case and process models. After selecting the model, the available operations, such as addition, are displayed in the Service model's Detail section.

The input and output parameters are pre-populated and can be defined using expressions:

Service Registry Task Configuration in Flowable Design