Definition block

About the definition block

The definition block starts with the keyword Definition and contains at least one variable definition statement. The variable definition statement can be:

VAR_NAME : VAR_TYPE

or

VAR_NAME [:VAR_TYPE] = EXPR

Where:

  • VAR_NAME indicates the variable name. It must be a valid identifier.
  • VAR_TYPE is the variable type that takes one of the following values: String, Number, Boolean, Date, Error, Field, Element or Object.
  • EXPR is an initialization expression that does not contain any action functions.
    An action function is a function that has side effects: it creates a business error, creates or modifies objects, elements or business properties. Action functions can only be used in the rule Action block.

Syntax notation

Definition statements are separated with a semicolon (‘;’) or a new line. A definition statement with an initialization section can span multiple lines: a line break is allowed after ‘=’; the initialization expression can span multiple lines (see Expressions).

Examples

Definition

myVar1: String

myVar2: Number = 15

myVar3 = 11 /* here the type of the variable is implied from the

initialization expression (it is Number) */

myVar5 = myVar2 + 1

myVar6 = myVar5 + (myVar2 - 1) / 2

myDate = toDate(“10/12/2012”, “dd/MM/yyyy”) /* here the type of the variable is implied from the return type of the function (it is Date)*/

 

// Variable definition on multiple lines

myVar7 = // after ‘=’

myVar2 * // after an operator

( // after an opening parenthesis

myVar3 + 2 // before a closing parenthesis

);

Variables

All the scalar variables (Number, String, Date, Boolean type) must be declared in the definition block. Object variables (variables that have properties) do not have to be declared in the definition block if they are used only in the action block after their initialization in the same action block.

Object variables are variables of FIELD, ERROR, OBJECT, ELEMENT type or variables initialized with the return value of getRecord or context functions.

Example

Definition

myVar1: String

myVar2: Object

If myVar1 Is Null And obj.PROP1 Is Not Null Then

myError = newError(“no arg”, “missing argument”) // this is correct; myError is of type ERROR

myError.field = “PROP1” // this is correct; the previous statement has defined the variable

myVarX = 1 // this is incorrect - a scalar variable has to be declared in the definition block

All the variables except the predefined variables have local scope (are seen only in the same rule).

A variable defined without an initialization section in the definition block will take the value of the argument with the same name declared the rule-based function.

Example

//the rule-based function has an argument “arg” of type “string”

Definition

arg: String; // will take the value of “arg” argument of the rule-based function

//….

 

Properties

A property can be a business property, a datasource column, a datasource record column, a business error property, a context property or a property of a User Interface field. The property can be referred to by prefixing the property name with the variable

VAR_NAME.PROPERTY_NAME

Where VAR_NAME contains the coll, obj, elt or ds predefined variables, or any variable of the FIELD, ERROR, OBJECT, ELEMENT or RECORD type.

Example

obj.PROP1 refers to the PROP1 property of the “obj” variable.

A property can be referred to only by its name without a prefix when there is no variable with the same name declared in the definition block and the business entity (Collection, Object, Element, Datasource) can be unambiguously determined.

Functions

A rule can call predefined function and functions of the ExecuteFunction exit type.

A function call has the following syntax:

[LIBRARY_NAME.]FUNCTION_NAME(zero or more arguments separated by comma)

The function arguments can be written in two ways:

  • arg1_value, arg2_value ….. , argn_value
  • arg1_name: arg1_value, arg2_name: arg2_value ….. argn_name: argn_value
Note   Use the second option when there are many arguments and some of them are not mandatory.

The library prefix can be omitted in the following cases:

  • The function is predefined.
  • The type is ExecuteFunction exit , the LIBRARIES field in the rule editor is filled and the function can be unambiguously determined (it only exists in one library in the list of libraries for that field).

The argument of the function call can be any expression that is compatible with the type of the function argument.

Examples:

date1 = toDate(“11/11/2011”, “dd/MM/yyyy”); // “toDate” is a predefined function

date2 = now(); // “now” is a predefined function

myNr = myLib.add(myNr * 2, 1); // the function “add” is defined in the library “myLib”

myNr = add(myNr * 2, 1); /* the function “add” is defined in the library “X”

and is not defined in the library “Y”, and the LIBRARIES field in the rule editor is “X,Y” */

myStr = upper(obj.PROP_STR); /* incorrect if “upper” is defined both in library “A” and “B”

and the LIBRARIES field in the rule editor is “A,B” */

myVar = myLib.myFunction(arg1: 1, arg2: 2, arg4: 4) // the function “myFunction” has four arguments (arg1, arg2, arg3-optional, arg4) and is defined in the library “myLib”

Expressions

An expression can be a literal, a variable, a property, a function call or a unary or binary operation of expressions (this is a recursive definition).

The operators are the following (in the descending order of their precedence):

  • unary minus(‘-‘),
  • multiplication(‘*’),
  • division(‘/’),
  • addition (‘+’) and
  • subtraction (‘-‘).

To enforce another precedence, parentheses can be used.

Example:

Definition

x: number

y: number

minusX = -x // unary minus

xAndHalfY = x + y / 2 // first divides y by 2, then adds the result to x

avgXY = (x + y) / 2 // first adds x and y, then divides by 2

//…..

An expression can span over multiple lines. A new line is allowed:

  • After an operator or an opening parenthesis.
  • Or before a closing parenthesis.

Related Links