Skip to main content

https://developer.salesforce.com/docs/atlas.en-us.232.0.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

 

According to the above document, since the roll-up summary field is calculated after both the before and after triggers are executed, the field values of Trigger.old and Trigger.new within the before trigger must be the same. But when I ran it, the two values came out differently, which is whether the document is wrong or I misunderstood?

 

In addition, there is no information about the formula field, so I want to know when it is calculated

1 answer
  1. Jan 7, 2025, 11:31 AM

    Hi @lee sihyeon

     

    According to the Salesforce documentation, Roll-up summary field are indeed calculated after both before Triggers and After Triggers have finished executing. This ensures that the values used in the calculation are up-to-date. 

     

    Within a before trigger, Trigger.new and Trigger.old should indeed have the same values for the Roll-up summary field, as the calculation hasn't occurred yet. If you are seeing different values, it might be due to:

    1. Other trigger or automation process updating the field before your trigger executes.

    2.A misunderstanding of how Trigger.old or Trigger.new works.

     

    Regarding the Formula Field: - Formula Field are calculated in real time, whenever the record is queried or updated. This means that the formula field value is recalculated every time the record is accessed. Formula fields are calculated after Before Triggers and before After triggers.

0/9000