Newer Version Available

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

FormulaInstance Class

Contains a method to evaluate the formula instance.

Namespace

FormulaEval

Example

1global class MotorYacht {
2   global Integer lengthInYards;
3   global Integer numOfGuestCabins;
4   global String name;
5   global Account owner;
6}
7
8MotorYacht aBoat = new MotorYacht();
9aBoat.lengthInYards = 52; 
10aBoat.numOfGuestCabins = 4;
11aBoat.name = 'RV Foo';
12FormulaEval.FormulaInstance isItSuper = FormulaEval.FormulaBuilder.builder()
13                            .withReturnType(FormulaEval.FormulaReturnType.STRING)
14                            .withType(MotorYacht.class)
15                            .withFormula('IF(lengthInYards < 100, "Not Super", "Super")')
16                            .build();
17isItSuper.evaluate(aBoat); //=> "Not Super"
18
19aBoat.owner = new Account(Name='Acme Watercraft', Site='New York');
20FormulaEval.FormulaInstance ownerDetails = FormulaEval.FormulaBuilder.builder()
21                            .withReturnType(FormulaEval.FormulaReturnType.STRING)
22                            .withType(MotorYacht.class)
23                            .withFormula('owner.Name & " (" & owner.Site & ")"')
24                            .build();
25ownerDetails.evaluate(aBoat); //=> "Acme Watercraft (New York)"
26

Usage

The withType context type must be a global, user-defined Apex class. Any fields or properties that the formula references must also be global.

FormulaInstance Methods

The following are methods for FormulaInstance.

evaluate(contextObject)

Calculates the formula expression and returns the formula output.

Signature

public Object evaluate(Object contextObject)

Parameters

contextObject
Type: Object
An instance of the Apex class as generated with the FormulaBuilder.builder() method.

Return Value

Type: Object

Apex type that corresponds to the Apex class as configured by the withType() method in the FormulaBuilder class.