FormulaBuilder Class

Contains methods to build and validate user-defined formulas.

FormulaEval

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.

The following are methods for FormulaBuilder.

  • build()

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

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

  • treatNumericNullAsZero(isNumericNullZero)

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

  • withFormula(formulaText)

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

  • withGlobalVariables(formulaGlobals)

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

  • withReturnType(returnType)

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

  • withType(contextType)

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

  • withType(contextType)

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

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

public FormulaEval.FormulaInstance build()

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.

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.

public formulaeval.FormulaBuilder parseAsTemplate(Boolean templateMode)

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

Type: FormulaEval.FormulaBuilder

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.

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 & ")"'.

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

public FormulaEval.FormulaBuilder treatNumericNullAsZero(Boolean isNumericNullZero)

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

Type: FormulaEval.FormulaBuilder

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

public FormulaEval.FormulaBuilder withFormula(String formulaText)

Type: FormulaEval.FormulaBuilder

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

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

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

Type: FormulaEval.FormulaBuilder

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

public FormulaEval.FormulaBuilder withReturnType(formulaeval.FormulaReturnType returnType)

Type: FormulaEval.FormulaBuilder

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

public formulaeval.FormulaBuilder withType(System.Type contextType)

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

Type: FormulaEval.FormulaBuilder

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

public formulaeval.FormulaBuilder withType(Schema.SObjectType contextSObjectType)

Type: FormulaEval.FormulaBuilder

This example uses an SObject type as an input in the withType() method to build and evaluate a formula.