2

I am writing a test class for a trigger (after update in this case), and while the code works on my person account record page when I inline edit, it does not work in my test class when I manually change the field that would re-evaluate the formula to true/false. The problem it seems is that the formula field does not get evaulated in Trigger.New while I'm executing the test class. here's where the code breaks:

Trigger code - this is where I get different results in inline vs. test class change. Enrolled_Within_Past_Year__pc is the formula field:

if(triggerNew.get(i).Enrolled_Within_Past_Year__pc != triggerOld.get(i).Enrolled_Within_Past_Year__pc){
 ....
 }

1 Answer 1

0

You cannot check if a formula has changed its value within Apex Trigger, Workflow Rule, or Process Builder. The value is calculated at retrieval time, so you can only ever get one value. What you are attempting is impossible.

See, for example, An Introduction to Formulas (emphasis mine):

Objects persisted in the database tend to have field types that store data, for example numbers or text. They can also have a formula field type, which is calculated at run-time much like formulas in a spreadsheet. Besides their use as a field type, formulas are used to determine when certain actions should occur on the platform, for example when a workflow rule should fire, and in validation rules that determine when data is bona fide.

3
  • 1
    Do you have a link supporting this behavior? Commented Sep 18, 2018 at 14:27
  • 1
    Thanks for the answer, Adrian. That is great information to learn from. I just want to know, why is it the case that when I do inline edit, the formula field value has been re-calculated and triggerNew.get(i).Enrolled_Within_Past_Year__pc != triggerOld.get(i).Enrolled_Within_Past_Year__pc equates to true? Commented Sep 18, 2018 at 15:01
  • 1
    adrian - please note: see salesforce.stackexchange.com/a/45346/2602
    – cropredy
    Commented Sep 19, 2018 at 22:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .