Newer Version Available
Formula Class
Namespace
Usage
Use the Formula class in conjunction with the FormulaBuilder and FormulaInstance classes in the FormulaEval namespace.
Example
This example creates a formula instance using Formula.builder() and the FormulaBuilder methods.
1FormulaEval.FormulaInstance ff = Formula.builder()
2 .withType(Account.SObjectType)
3 .withReturnType(FormulaEval.FormulaReturnType.STRING)
4 .withFormula('{!name} ({!website})')
5 .parseAsTemplate(true)
6 .build();Formula Methods
The following are methods for Formula.
builder()
Signature
public static formulaeval.FormulaBuilder builder()
Return Value
recalculateFormulas(sobjects)
Signature
public static List<System.FormulaRecalcResult> recalculateFormulas(List<SObject> sobjects)
Parameters
- sobjects
- Type: List<SObject>
- List of sObjects whose formula fields are to be recalculated.
Return Value
Type: List<FormulaRecalcResult Class>
Usage
Recalculate formula fields on new or queried SObjects. If all data is present on the SObjects, SOQL limits are not affected. If the data required to evaluate a formula field is missing, that data is retrieved and limits are changed accordingly.
The new formula values are stored in the SObjects themselves and overwrite previous values of formula fields.
Example
1Account a = new Account();
2a.Name = 'Salesforce';
3a.BillingCity = 'San Francisco';
4List<Account> accounts = new List<Account>{a};
5
6List<FormulaRecalcResult> results = Formula.recalculateFormulas(accounts);
7System.assert(results[0].isSuccess());
8// Option 1
9System.debug('New value: ' + accounts[0].get('My_Formula_Field__c'));
10// Option 2
11System.debug('New value: ' + results[0].getSObject().get(‘My_Formula_Field__c’));