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.
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 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
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:
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.
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
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())
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
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
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.
Click on Finish button
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.:
Call the method (optionally instantiate the class if method is not static):
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.
You can add logging to Groovy scripts or Java code that you develop.
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.