Getting started with jBPM Script Tasks

Script tasks allow you to execute custom scripts or code snippets as part of a jBPM process flow. This can be useful for performing complex calculations, accessing external data sources, or integrating with other systems. In this tutorial, we will look at how to create and use script tasks in jBPM.

Prerequisites

To follow this tutorial, you should have the following:

Creating a Script Task

To create a script task in jBPM, follow these steps:

  1. In the jBPM Designer, open the process diagram for the process in which you want to use a script task.
  2. Drag the “Script Task” icon from the palette onto the canvas. This will create a new script task in the process.
  3. Double-click on the script task to open the task properties window.
  4. In the “Script” field, enter the code that you want to execute as part of the script task. You can use any of the supported scripting languages, such as JavaScript, Groovy, or Python.
  5. (Optional) In the “Script Format” field, specify the language that you are using for the script. If you don’t specify a script format, jBPM will try to detect the language based on the script’s syntax.
  6. (Optional) In the “Inputs” and “Outputs” fields, you can specify any input or output variables that the script task needs to use. These variables can be defined in the process variables or as process data objects.
  7. Click “Save” to apply your changes and close the task properties window.

Here is a sample Script Task:

jbpm script task tutorial

Executing a Script Task

To execute a script task in a jBPM process, simply include it in the process flow and start the process. The script task will be executed when it is reached in the flow, and any output variables will be available for use in subsequent tasks or processes.

Typically, in a Script task you will be managing the KieContext interface, that is the main Interface for the ProcessContext and RuleContext:

Example 1: How to access the current node instance. You can also query the node instance for data, such as its name and type. You can also cancel the current node instance.

NodeInstance node = kcontext.getNodeInstance();
String name = node.getNodeName();

Example 2: How to get the current Process Instance. From there, you can for example send a signal to the Process:

ProcessInstance proc = kcontext.getProcessInstance();
proc.signalEvent( type, eventObject );

Example 3: How to set/get Process variables from a Script Task. In this example, we use a script task to update the value of a process variable called discountAmount based on the value of another process variable called totalAmount:

if (kcontext.getVariable("totalAmount") > 100) {
  kcontext.setVariable("discountAmount", 10);
} else if (kcontext.getVariable("totalAmount") > 50) {
  kcontext.setVariable("discountAmount", 5);
} else {
  kcontext.setVariable("discountAmount", 0);
}

Example 4: In this example, we use a script task to update the values of a process data object called customer:

var customer = kcontext.getKieRuntime().getDataService().find("customer", kcontext.getProcessInstanceId());
customer.setFirstName("John");
customer.setLastName("Doe");
kcontext.getKieRuntime().getDataService().update("customer", customer);

The kcontext variable provides access to the KieRuntime object, which in turn provides access to the data service. We use the find method to retrieve the customer data object, and then use the set methods to update its values. Finally, we use the update method to save the updated object back to the data store.

Example 5: Aborting or completing the process:

// Abort the process
kcontext.setAbortProcess(true);

// Complete the process
kcontext.setExitProcess(true);

Example 6: Accessing the process context:

// Get the process context
var processContext = kcontext.getProcessContext();

// Set a value in the process context
processContext.set("key", "value");

// Get a value from the process context
var value = processContext.get("key");

The process context is useful for storing and retrieving data that needs to be shared across different nodes in the process, or that needs to be persisted beyond the lifetime of a single process instance.

Conclusion

Script tasks are a powerful tool for adding custom logic to jBPM process flows. Whether you need to perform complex calculations, access external data sources, or integrate with other systems, script tasks can help you extend the capabilities of your jBPM processes.

Found the article helpful? if so please follow us on Socials