Functions in expressions
Functions extend the power of
Pega Platform
expressions. An expression can contain many function calls, including calls to built-in functions, standard functions, and custom functions that you create. Use the Function Builder to help find the function that you need.
This topic relates to creating and using Java functions. For information about creating and using SQL functions in Report Definition rules, see
About Report Definitions
and
Report Definition Rules — Using the Calculation Builder.
The following forms call a function. The fully qualified syntax for calling a function identifies both the ruleset and the
library: For example: Using fully qualified references eliminates the risk of function overrides by a library or
ruleset. By fully qualifying a ruleset, standard ruleset resolution does not apply. Double-check
your requirements before using this type of function call.
When using this syntax, "ruleset" may be omitted (defaults to
The library qualified syntax for calling a function identifies the library name: Using library qualified references provides a more readable syntax than the fully qualified
syntax, and prevents accidental library conflicts that can occur with unqualified calls. For example, if two libraries with the same name are located in different rulesets, when
using library qualified notation, the library from the ruleset listed higher in the operator
runtime ruleset list is selected. Additionally, using this notation helps to avoid multiple "suitable found" instances, such
as when the same function exists in two different libraries but in the same ruleset. With library qualified references, standard ruleset resolution applies, allowing the
function to be resolved from any ruleset. This provides the capability to override a
function in a different ruleset name, similar to how other rule types can be overridden. The unqualified syntax for calling a function calls a function rule: The unqualified syntax is the simplest to read, but does not identify a ruleset or a
library for the function. Your system may contain many functions that match the function
name.
Pega Platform
uses your ruleset list to find the best one to execute,
using the following algorithm:
Functions when compiled extend the Java class: where the ruleset name and library name are converted to lowercase. To call a function from
within Java, use the following syntax: using the exact case for the function name. For example, to call the CardType() function in the Sort library of the MyInventory
ruleset, use:
Pega Platform
constructs the Java class name for a
Rule-Utility-Library
by concatenating the ruleset name, an underscore
and the library name, with the resulting string converted to lowercase and characters not
valid in a
Java
identifier
translated to an underscore.
Four functions are part of the expression language. All other names following an at-sign are references to function rules, instances of the
Rule-Utility-Function
rule type. An expression can use any of these built-in functions:
Equivalent to the Java ternary operator “?:.” Only the one appropriate result parameter is evaluated.
You can use a
These basic arithmetic functions are available.
You can use a
The following functions operate with date and time values. A
The standard function
callActivity( )
in the Pega-RULES
Utilities
library calls an activity, but returns only void, not a value result. You can execute an activity in an expression, but only for its side effects. Identify the primary page, the activity, and the parameter page. For example:
Pega Platform
allows multiple function rules to be defined with the same name, in the same ruleset, version, and library, if they have different signatures. The signature of a function is computed automatically from the name, the position and data types of parameters, and the return type. Functions that have multiple variants are called overloaded.
When calling an overloaded function, you do not need to specify which variant to use. The system determines which to use based on the types and positions of parameters you supply. For example, assume one function named Round is defined with two variants: If your call includes two parameters, the system uses the second variant.Methods for calling a function
Fully qualified function call
@(RuleSet:libraryname).functionname(arg1, arg2... argn)
@(MyInventory:Sort).CardType("Gold")
lib(ruleset:libraryname).functionname(arg1, arg2... argn)
Pega-RULES
), and "libraryname" may be omitted (defaults to
default
).
Library qualified function call
@LibraryName.FunctionName(arg1, arg2... argn)
Unqualified function call
@FunctionName(arg1, arg2... argn)
double
for the preferred
BigDecimal
type if necessary to get a match.
Java call
com.pegarules.generated.
myruleset_mylibrary
result = com.pegarules.generated.
myruleset_mylibrary.MyFunction( params )
myinventory_sort.CardType("Gold")
Built-in and When() functions
Syntax Description These functions are part of the expression language. Their names are reserved and cannot be overridden. These functions may only be invoked using the preferred (‘@’) syntax. @if( boolean expression, result1, result2)
Evaluates the condition in parameter one and then, if true, evaluates and returns parameter two, otherwise evaluates and returns parameter three.
@and( e1, e2, ...)
Logical AND operation of the (arbitrary number of) parameters (Evaluated left-to-right, stopping when the result is known) @or( e1, e2, ...)
Logical OR operation of the (arbitrary number of) parameters (Evaluated left-to-right, stopping when the result is known) General purpose @when(name)
Evaluates a when condition rule in the context of the primary page of the rule in which the expression appears. The parameter can be an expression that evaluates to a text value. @when(name, pagename)
Evaluates the specified when condition rule using the supplied
ClipboardPage
PublicAPI object (not a page name) as the primary page for evaluation. Either parameter can be an expression that evaluates to the correct data type.
Value List
,
Value Group
, or
Page Group
property as an argument to the @SUM(), @MIN(), @MAX() and @AVERAGE().
Arithmetic functions
Syntax Description Scalar arithmetic @abs(num)
Absolute value @sqr(num)
Square @sqrt(num)
Square root @exp(num)
Exponential @ceil(num)
Ceiling, least integer not greater than @floor(num)
Floor, greatest integer not less than @round(num)
Rounded to the nearest integer Aggregate arithmetic @sum(num1, num2)
Sum @max(num1, num2)
Maximum @min(num1, num2)
Minimum @mode(num1, num2)
Modal value @stddev(num1, num2)
Standard deviation @average(num1, num2)
Average Value List
,
Value Group
, or
Page Group
property as an argument to the @SUM(), @MIN(), @MAX() and @AVERAGE().
DateTime functions
DateTime
property type represents an instant in time while
Date
and
TimeofDay
property types represent values. Adding (the BigDecimal/double values corresponding to) a
Date
and a
TimeofDay
together similarly represents an instant in time, but in an unknown or arbitrary time zone.
Syntax Description These Rule-Utility-Function rules are similar to the corresponding functions in Microsoft Excel. @date(year, month, day)
Returns a BigDecimal value (
Pega Platform
Date) corresponding
to the int parameters specified. The year is a four digit year, and is not adjusted
for a “base date.” The parameters are interpreted leniently (as in Microsoft Excel)
through the use of the Java Calendar class.
@datevalue(string)
Returns a BigDecimal value (
Pega Platform
Date) corresponding
to the parsed value of the string specified. Only the date portion of the string is
considered.
@day(double) @day(BigDecimal))
Returns the int value corresponding to the day of the month (1 to 31) when the
double parameter is interpreted as a
Pega Platform
Date.
@month(double)
@month(BigDecimal)
Returns the Java int value corresponding to the month of the year (0 to 11) when
the double parameter is interpreted as a
Pega Platform
Date. The
corresponding Excel function also returns 0 to 11.
@today()
@today(selection)
Returns the BigDecimal value (
Pega Platform
Date)
corresponding to the current date.
@weekday(double)
@weekday(BigDecimal)
Returns the int value corresponding to the day of the week (Sunday = 1 to
Saturday = 7) when the double parameter is interpreted as a
Pega Platform
Date.
@year(double)
@year(BigDecimal)
Returns the int value corresponding to the year when the double parameter is interpreted as a
Pega Platform
Date.
@hour(double)
@hour(BigDecimal)
Returns the Java int value corresponding to the hour of the day (0 to 23) when the double parameter is interpreted as a
Pega Platform
Time of Day.
@minute(double)
@minute(BigDecimal)
Returns the Java int value corresponding to the minute of the hour (0 to 59) when the double parameter is interpreted as a
Pega Platform
Time of Day.
@second(double)
@second(BigDecimal)
Returns the Java int value corresponding to the second of the minute (0 to 59) when the double parameter is interpreted as a
Pega Platform
Time of Day.
@time(hour, minute, second)
Returns a BigDecimal value (
Pega Platform
Time of Day)
corresponding to the int parameters specified.
@timevalue(string)
Returns a BigDecimal value (a
Pega Platform
Time of Day value) corresponding to the parsed value of the string specified. Only the time portion of the string is considered.
The following functions facilitate conversion between BigDecimal/double values derived from DateTime properties and Date or Time of Day properties.
Use these functions to adjust the double value resulting from the sum of a Date and Time of Day, or the double value resulting from a DateTime property into a (possibly different) known time zone. The return type of these functions is the same as the type of the first argument. @toGMT(double, zone)
@toGMT(BigDecimal, zone)
Returns a double/BigDecimal value (
Pega Platform
DateTime) in
the GMT time zone corresponding to the double value interpreted in the “zone” time
zone. Use to convert the sum of a Date and Time of Day to a DateTime. If zone is null,
then the time zone of the requestor is used.
@toLOCAL(double, zone)
@toLOCAL(BigDecimal, zone)
Returns a double/BigDecimal value that is the result of treating the double value as a
Pega Platform
DateTime (in the GMT time zone) and converting it to the time zone. You can then treat the resulting value as Date and Time of Day values in the specified time zone. If zone is null, then the time zone of the requestor session is used.
For example, if bostonTime is a double value that represents a date and time in the Eastern Standard Time zone, you can find the corresponding time in India by using:
toLOCAL(toGMT(bostonTime, “EST”), “IST”).
Calling an activity in an expression
@Pega-RULES:Utilities.callActivity(pyWorkPage, MyActivity, tools.getParameterPage());
Calling an overloaded function
Previous topic Constants in expressions Next topic Displaying all functions in the Expression Builder