@Daniel Ballinger I've found a problem with the FormulaEval feature, and custom lookups. Is this a current limitation?
It works fine with standard lookups, but when trying to use custom lookups we get a GACK back.
To reproduce...
1. Create a custom lookup field on the Contact object to an Account, e.g. Other_Account__c.
2. Create a contact and populate both the Account (standard lookup) and Other_Account__c (custom lookup) with the same record.
3. Run the following apex code, changing the Id in the query to the contact id you just created.
Contact theContact = [SELECT Account.Name, Other_Account__r.Name FROM Contact WHERE Id ='003JX00000QMo5dYAD'];
FormulaEval.FormulaInstance formulaInstance = Formula.builder()
.withType(Contact.SObjectType)
.withFormula('Account.Name')
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.build();
System.debug(formulaInstance.evaluate(theContact));
--- THIS WILL WORK FINE AND THE DEBUG WILL SHOW THE ACCOUNT NAME.
--- NOW CHANGE THE FORMULA TO Other_Account__r.Name FROM Account.Name
FormulaEval.FormulaInstance formulaInstance = Formula.builder()
.withType(Contact.SObjectType)
.withFormula('Other_Account__r.Name')
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.build();
System.debug(formulaInstance.evaluate(theContact));
--- YOU NOW GET AN EXCEPTION IN THE LOGS
Error on line 23, column 1: System.UnexpectedException: Salesforce System Error: 250844729-248762 (162743969) (162743969)
(System Code)
Yes, those always worked, but you have to add them to the code...
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_formulaeval_FormulaGlobal.htme.g.
FormulaEval.FormulaInstance ff = Formula.builder()
.withType(Schema.Account.class)
.withReturnType(FormulaEval.FormulaReturnType.STRING)
.withGlobalVariables(new List<FormulaEval.FormulaGlobal>{FormulaEval.FormulaGlobal.USER})
.withFormula('$User.Id')
.build();
system.debug(ff.evaluate(new Account()));