Newer Version Available

This content describes an older version of this product. View Latest

FormulaBuilder Class

Contains methods to build and validate user-defined formulas.

Namespace

FormulaEval

Usage

The context type that corresponds to the Apex class used in the builder withType() method must be a global, user-defined Apex class. Any fields or properties that the formula references must also be global.

FormulaBuilder Methods

The following are methods for FormulaBuilder.

build()

Validates and returns the formula instance created using the FormulaBuilder methods.

Signature

public FormulaEval.FormulaInstance build()

Return Value

Type: FormulaEval.FormulaInstance

Returns an instance of the FormulaInstance object. If the formula validation such as field references, functions, or syntax, fails, the method throws a FormulaValidationException exception.

parseAsTemplate(templateMode)

Optional. Indicates whether a formula expression created with the build() method is evaluated in template mode. In template mode, values are interpolated into a string by using merge field syntax rather than by concatenating strings with the & operator. Merge fields use the syntax {!Object_Name.Field_Name}, where names are preceded by an exclamation mark and enclosed in curly braces.

Signature

public formulaeval.FormulaBuilder parseAsTemplate(Boolean templateMode)

Parameters

templateMode
Type: Boolean
If true, the formula expression is evaluated in template mode. The default value is false.

Return Value

Type: FormulaEval.FormulaBuilder

Usage

In template mode, the FormulaEval.FormulaReturnType value that’s set with withReturnType() must be STRING.

Template mode supports the same global variables, formula expressions, and context types as non-template mode, as long as they are correctly set using the FormulaBuilder methods.

Example

In this example, true is passed to parseAsTemplate(). The formula expression is evaluated in template mode, so the values of the name and website fields on the Account record are interpolated into the string using merge field syntax. The output is equal to the expression 'name & " (" & website & ")"'.

1FormulaEval.FormulaInstance ff = Formula.builder()
2    .withType(Schema.Account.class)
3    .withReturnType(FormulaEval.FormulaReturnType.STRING)
4    .withFormula('{!name} ({!website})')
5    .parseAsTemplate(true)
6    .build();

treatNumericNullAsZero(isNumericNullZero)

Optional. Indicates whether a null for a numeric data type is treated as zero while evaluating the formula with the build() method.

Signature

public FormulaEval.FormulaBuilder treatNumericNullAsZero(Boolean isNumericNullZero)

Parameters

isNumericNullZero
Type: Boolean
If true, null for numeric is treated as zero. The default value is false.

Return Value

Type: FormulaEval.FormulaBuilder

withFormula(formulaText)

Required. Sets the formula expression that the build() method uses to create the formula instance.

Signature

public FormulaEval.FormulaBuilder withFormula(String formulaText)

Parameters

formulaText
Type: String

Return Value

Type: FormulaEval.FormulaBuilder

withGlobalVariables(formulaGlobals)

Optional. Sets the list of global variables that can be referenced in the formula expression created with the build() method.

Signature

public FormulaEval.FormulaBuilder withGlobalVariables(List<formulaeval.FormulaGlobal> formulaGlobals)

Parameters

formulaGlobals
Type: List<FormulaEval.FormulaGlobal>
Uses values from the FormulaGlobal enum.

Return Value

Type: FormulaEval.FormulaBuilder

withReturnType(returnType)

Required. Sets the formula output data type for the formula instance created with the build() method.

Signature

public FormulaEval.FormulaBuilder withReturnType(formulaeval.FormulaReturnType returnType)

Parameters

returnType
Type: FormulaEval.FormulaReturnType
Uses values from the FormulaReturnType enum.

Return Value

Type: FormulaEval.FormulaBuilder

withType(contextType)

Sets the Apex type that corresponds to the Apex class used with the build() method.

Signature

public formulaeval.FormulaBuilder withType(System.Type contextType)

Parameters

contextType
Type: System.Type
An instance of the Apex class type.

Return Value

Type: FormulaEval.FormulaBuilder

withType(contextType)

Sets the Apex type that corresponds to the Apex class used with the build() method to SObject type.

Signature

public formulaeval.FormulaBuilder withType(Schema.SObjectType contextSObjectType)

Parameters

contextSObjectType
Type: Schema.SObjectType
An instance of the SObject type.

Return Value

Type: FormulaEval.FormulaBuilder

Example

This example uses an SObject type as an input in the withType() method to build and evaluate a formula.
1FormulaEval.FormulaInstance ff = Formula.builder()
2    .withReturnType(FormulaEval.FormulaReturnType.Boolean)
3    .withType(Account.SObjectType)
4    .withFormula('ISBLANK(Site)')
5    .build();
6
7Boolean siteIsBlank = (Boolean)ff.evaluate(new Account(Site='Test'));
8Assert.isFalse(siteIsBlank);