Skip to main content


         This documentation site is for previous versions. Visit our new documentation site for current releases.      
 

Functions in expressions

Updated on May 31, 2022

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.

Methods for calling a function

The following forms call a function.

Fully qualified function call

The fully qualified syntax for calling a function identifies both the ruleset and the library:

@(RuleSet:libraryname).functionname(arg1, arg2... argn)

For example:

@(MyInventory:Sort).CardType("Gold")

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.

Note: A deprecated syntax used before Version 04-01 is still available as an alternative to using a fully qualified function call:
lib(ruleset:libraryname).functionname(arg1, arg2... argn)

When using this syntax, "ruleset" may be omitted (defaults to Pega-RULES ), and "libraryname" may be omitted (defaults to default ).

Library qualified function call

The library qualified syntax for calling a function identifies the library name:

@LibraryName.FunctionName(arg1, arg2... argn)

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.

Unqualified function call

The unqualified syntax for calling a function calls a function rule:

@FunctionName(arg1, arg2... argn)

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.

Note: When using this syntax, if another developer creates a function with the same name and signature in another library or ruleset, their call may be affected.

Pega Platform uses your ruleset list to find the best one to execute, using the following algorithm:

  1. Collect a list of the Rule-Utility-Function rules belonging to the rulesets listed in the user's ruleset list that have the same name and same number of parameters.
  2. Exclude functions defined in any library named default. (The use of the default library is deprecated.)
  3. If the list is empty, no such functions exist. The system reports an error.
  4. If more than one such function exists (because the same name is overloaded), select the best match by comparing the data types of the parameters, using the promotion rules for Pega Platform data types.
  5. If the Rule-Utility-Function rule has not supplied data types for its parameters or return value, use standard Java promotion rules to match parameters. As a last resort, substitute double for the preferred BigDecimal type if necessary to get a match.
  6. If more than one function matches the selected name and fully decorated signature, then the function from the higher listed ruleset is executed.

Java call

Functions when compiled extend the Java class:

com.pegarules.generated. myruleset_mylibrary

where the ruleset name and library name are converted to lowercase. To call a function from within Java, use the following syntax:


        result = com.pegarules.generated.
        myruleset_mylibrary.MyFunction( params )

using the exact case for the function name.

For example, to call the CardType() function in the Sort library of the MyInventory ruleset, use:

myinventory_sort.CardType("Gold")

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.

Built-in and When() functions

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:

SyntaxDescription
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.

Equivalent to the Java ternary operator “?:.” Only the one appropriate result parameter is evaluated.

@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.
Note: The primary page for evaluation is supplied as a ClipboardPage object, not a page name, so that embedded pages (or pages whose name is not known) can be evaluated.

You can use a Value List, Value Group, or Page Group property as an argument to the @SUM(), @MIN(), @MAX() and @AVERAGE().

Arithmetic functions

These basic arithmetic functions are available.

SyntaxDescription
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

You can use a Value List, Value Group, or Page Group property as an argument to the @SUM(), @MIN(), @MAX() and @AVERAGE().

DateTime functions

The following functions operate with date and time values. A 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.

SyntaxDescription
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

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-RULES:Utilities.callActivity(pyWorkPage, MyActivity, tools.getParameterPage());

Calling an overloaded function

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:

  • Round(double: d) — Returns an int by rounding the input value d, a double
  • Round(double: d, places: n) — Returns a double by rounding the value to n decimal places

If your call includes two parameters, the system uses the second variant.

Have a question? Get answers now.

Visit the Support Center to ask questions, engage in discussions, share ideas, and help others.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best.

Pega.com is not optimized for Internet Explorer. For the optimal experience, please use:

Close Deprecation Notice
Contact us