0

Writing a trigger to handle updating a few fields on a record (of the same object, this is an Account to an Account lookup) when it is attached via a lookup relationship (ie. it is the parent or looked up to record), the trigger is an aftertrigger actually but my question more concerns general trigger behavior.

My expectation was that the Child record would fire first considering that is the record actually being changed. I honestly wasn't sure if the parent record being attached would even fire an update trigger but checking the debug logs, that does not seem to be the case, it appears the BeforeUpdate trigger for the parent record is the one firing first, then a BeforeUpdate for the child, then AfterTrigger for parent, BeforeUpdate for child.

This Org is a bit messy with a significant amount of managed packages and a a lot of workflow rules so question is if this is standard behavior or i have something odd happening in my org. Spent a good amount of time googling but didnt find much on this question. If its standard behavior i can certainly design my trigger to assume the initial record coming in will be the parent record (again it is a lookup relationship being created between two objects of the same type)

1

The default behavior would be the child gets a Before Update trigger, then a After Update trigger. The parent record would not ordinarily fire. This is the vanilla behavior.

I'm presuming you've read the logs wrong, because the absolute first trigger to fire would need to be the child's Before Update trigger. From there, all bets are off. A before update trigger could cause an update to the parent, which might even be recursive up the hierarchy. Or it could be an After Update trigger that does this.

The logs are generally pretty hard to read in highly customized orgs, but you might try reading the logs using the Developer Console's enhanced views (Debug > Change Perspective > All) that provides a "time line" view of what happened when.

Also, you will want to read Triggers and Order of Execution for more information on the way a transaction saves.

  • Im reading the logs in VSC and they have detailed time stamps. I misinterpreted them to a degree as the BeforeUpdate does fire first on the child, additional logic causes beforeUpdate to fire on the Parent and then AfterUpdate is fired on the Parent first, which is causing my issues – Bobbygllh Nov 16 at 22:28
  • @Bobbygllh That would be the expected order. If a trigger on the child causes an update to the parent, both the before and after triggers will occur on the parent before going back to the child. The Triggers and Order of Execution document behavior is always followed. – sfdcfox Nov 16 at 23:14

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.