4

I've inherited an org where there are two triggers on the same object that definitely both run after insert and after update. Completely against SFDC prevailing advice of course.

So I know the order of execution is indeterminate. Let's say on a particular execution that Trigger A runs first and Trigger B runs second. Let's also say that Trigger A updates the same record (and that it's smart enough not to fall into infinite recursion). What I'm wondering is: When Trigger B executes for the first time, will it see in Trigger.new the same original values that Trigger A also saw in Trigger.new? Or will it see the values that Trigger A updated?

4

All triggers see the records from the point they were loaded in the database. In other words, regardless of A or B running first, they will not see any affect of the other's execution on Trigger.new records. This is how salesforce.com simulates the triggers running "simultaneously," even though they actually operate one after the other. Be aware, though, that if both triggers modify the same field in Trigger.new, the effects are undefined (typically, the trigger created later wins, but there is no guarantee to this order). If there is a recursive call back to the same records, A and B will run again, and both will see the same updated values. Records besides those in Trigger.new that are modified will appear in the other trigger's context if you query for them.

  • Thank you. One trigger contains a guard condition to prevent the trigger from firing on it a second time, and flags that guard when it updates the record. The second trigger does other stuff but also flags that guard condition on all records that pass through it. (I didn't write this code, just reporting...) So I was concerned that if the second trigger flags the guard condition first it could prevent the first trigger from running. Sounds though like both triggers will see the same data. – Charles T May 23 '17 at 16:14
  • @CharlesT if they are static variables, then you'd have a right to be concerned. Static variables can be seen across triggers, which could have undesirable side effects – sfdcfox May 23 '17 at 16:27
  • When I say "flag" I mean a field on the actual data record. Since both triggers are looking at trigger.new not the actual record, it should remain the case that both triggers see the unmodified flag the first time around. – Charles T May 23 '17 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.