Salesforce Trigger Context

At work we are working on a new Salesforce organization. We’ve written some code over the last few months, but at the advice of some contractors who are more seasoned with Apex, we are doing some refactoring of the code. One of these changes is making sure that recursive triggers are not possible – something along the lines of Account AFTER INSERT being called, which updated a Contact, which in turn might update the Account again.

We were a little stumped as to why this was necessary. I have been working in Apex on and off for six years now, but I never dug deep enough to understand what happens when nested triggers are called on different objects in the same transaction. So, I spun up a clean dev org to figure out how this works. In particular, I wanted to see if static variables persisted between the different triggers executing. Say you had the following:

  1. An Account is created, which calls the BEFORE INSERT trigger for the Account
  2. The trigger for the Account causes a Contact to be created. (This is a bad example, but the Contact is not tied to the account. In order to do so, this would be done in the AFTER INSERT trigger)
  3. The Contact is inserted, which calls the Contact BEFORE INSERT trigger.

It turns out that any static state that is done in the handler for the Account trigger, is still available to the Contact handler.

The results, after inserting a single Account:

Leave a Reply

Your email address will not be published. Required fields are marked *