This content is dedicated to our next version. It is work in progress: its content will evolve until the new version is released.

Before that time, it cannot be considered as official.

Process Expressions and scripts

Learn how to define Expressions and script in Bonita Studio that will be executed in your processes.

Expressions and scripts are used in Bonita Studio for many purposes, including setting the initial or default value of a variable. An expression is a simple statement; a script is a sequence of expressions, and has a program-like structure.

This page explains the features of Bonita Studio that enable you to use expressions and scripts.

Note that the expression editor cannot be used in the UI Designer, which has a different concept model for data.

Groovy scripts are meant to be used in processes only, do not use them in custom connectors or filters !

Custom connectors and filters should be considered as standalone/side projects and should preferably be created via Bonita filter archetype and Bonita connector archetype

Expression types

The expression editor is used throughout Bonita Studio to create and modify expressions or scripts.
To start the expression editor, click the crayon icon next to the field where you want to enter an expression.

There are different two types of expression:

  • Script: the result of the script sets the value of the expression.

  • Query: Use a query from your BDM.

Scripts

Scripts provide the most flexible to define business rules. Bonita uses Groovy scripts.

You can press on Ctrl + Space to access the auto-complete feature. It can be pressed several time to access all auto-complete proposals and templates

Use variables in a script expression

You can use a variable in an expression. When the expression is evaluated during process execution, the current value of the variable is used in the expression.

The following variables are available:

  • Parameters

  • Contract Input

  • Process Variables

  • Business Variables

  • Business Queries

  • Documents

  • Execution Context

  • Code Templates

All the variables are displayed in a searchable tree next to the script. To add a variable to the script, double click on it or drag and drop it on the script.

Execution Context

The execution context contains variables that are relevant to the current state of the system when evaluating this expression.

The provided variables are:

  • activityInstanceId: the identifier of the activity instance (not available for a process-level expression)

  • processDefinitionId: the identifier of the process

  • processInstanceId: the identifier of the process instance

  • rootProcessInstanceId: for a called process or an event subprocess, the identifier of the root process (note that if there are multiple layers of called processes or subprocesses, this is the root of the hierarchy, not the parent called process or subprocesses)

The provided variables list also contains a special variable, apiAccessor. This enables you to construct API calls in a script using autocompletion. For example, to get the number of overdue open tasks, choose apiAccessor from the list of provided variable, then add the processAPI, and then add getNumberOfOverdueOpenTasks.

Basic Operations

You can use the quick access operators bar above the script to easily write your expression.

Here are some examples.

Example 1: Do transition only if the change cost of my BDM object "Change request" is lower than 300.

return itemChangeRequest.changeCost <= 300

Example 2: Initialize a business data from an ID

def questionnaireVar = questionnaireDAO.findByPersistenceId(editquestionnaireInput.persistenceId_string.toLong())

User defined functions

Defining Groovy scripts inside the Bonita project is deprecated since 2022.1. We recommend using custom library extensions in your project instead.

You may still use your existing scripts when upgrading from a previous version, but you can’t create new ones and should move them into a custom extension.

If a same piece of script code is needed in different locations you might want to define it once and reused it in order to avoid duplication.

In order to reuse some Groovy code you need to:

  • Create a Groovy class that will be stored as part of your project in Bonita Studio (Removed in 2021.2)

  • Declare in the Groovy class one or several methods to store your code

  • Configure your process(es) dependencies to include the required Groovy script file(s)

  • In the expression editor, select the Script type and as part of your code call the method(s) declared previously

Configure process dependencies

If you plan to use a Groovy method, for example to process the output of a connector, you first need to add the Groovy script file as a dependency of your process:

  • Select your process pool

  • In the Bonita Studio coolbar, click on Configure

  • Select Java dependencies

  • In the tree view, under Groovy scripts, select the file(s) that define the method(s) you want to use (e.g. com/mypackage/MyClass.groovy)

  • Click on Finish button

Use a Groovy method

In order to call a Groovy method from a script defined using the expression editor you need to:

  • Add the import statement at the beginning of the script. E.g.: import com.mypackage.MyClass

  • Call the method (optionally instantiate the class if method is not static): MyClass.myMethod("test")

Update of process dependencies and package import can be automatically done when using code completion (this is trigger by default with the shortcut ctrl+space).

Note that the Groovy script will be embedded in the process deployment file (*.bar). If you update the Groovy script content you will need to redeploy the process in order to benefit from the modification.

Log messages in a Groovy script

You can add logging to Groovy scripts or Java code that you develop.

Scripts in right operands of operations at task level

Scripts can be used to define the result of the right operand of an operation. Those scripts are created in the same editor as the others, and can also call external methods and resources, but are designed as read-only scripts in the product.

It means that trying to directly write data to the database in those scripts (using java API methods), while it might work, is considered as a bad practice, and the behaviour of those scripts is not guaranteed across versions of the product. Data in this case, refers to documents, business objects, pages, process commentaries.

For documents, you should use the document type provided in the Studio and the associated operations related to this document type. For business objects, you should use the BDM type provided in the Studio and the associated operations related to this BDM type. For other use case you may want to use a connector to perform those write operations.