Skip to main content

At first, I was really excited about this feature because I often use formulas in my developments, such as in triggers to access parent records values. This way, I could avoid an extra query, for example, accessing the Account.Name in Case before insert. But now, trying out this new feature, I'm a bit disappointed because I'm getting null for this specific case.

 

trigger CaseTrigger on Case (before insert) {

    FormulaEval.FormulaInstance nameFormula = Formula.builder().withReturnType(FormulaEval.FormulareturnType.STRING).withType(Case.SObjectType).withFormula('Account.Name').build();

   

    for (Case cs : Trigger.New) {       

System.debug('Apex Formula: ' + nameFormula.evaluate(cs));

        System.debug('Formula: '+cs.Account_Name_Formula__c);

    }

}

Evaluate Dynamic Formulas in Apex

Is it on the roadmap to cover this?

답변 3개
  1. 2024년 6월 20일 오후 6:36

    Apex triggers don't bring back all the related records implicitly be design. If you were to debug out the Case objects in the Trigger.New list they would have the AccountID populated, but the Account reference would be null. 

     

    As mentioned, you would need a single bulkified SOQL query to pull in the details for the related Accounts. 

     

    Formulas in Apex will not full that data in for you. By design it only operates on data that is already in memory. 

0/9000