function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joey HoJoey Ho 

order of trigger execution logic on a list of inserts

Hi All,

 

this may be a silly question but I am going to ask and confirm my understanding of the order in which triggers are executed when a list of records are inserted.

 

This is my current proposed plan on my code in pseudo:

 

  • A list emal message records are inserted into Salesforce (relating to one case), i.e. more than one email inserted at the same time and updated on case - which I know is rare but it's good practice to bulkify my trigger.
  • When the list of email inserted, a BEFORE INSERT trigger and fired whick updates a string COMMON FIELD on the CASE.
  • An AFTER INSERT than attempts to call a class method that sends out SingleEmailMessage (that includes this COMMON FIELD in the email message) to the Case Team updating everyone  on the new Email Message Record that is inserted.

My question is this:

 

If the it so happens that we have a list of Email Records inserted at the same time will:

 

a) the list execute each Email Message Record individually, i.e. run the particuarly Email Message Record until it completes the BEFORE & AFTER INSERT Code meaning my COMMON FIELD that is being updated by every insert is unique to every new Email Message record?

 

b) Or does the list execute the inserts of the Email Message record list all at the same time such that it fires BEFORE INSERT first on all records, than fire the AFTER INSERT after which will evidentily cause issues where the COMMON FIELD when its being updated?

 

Thanks for your help!

 

Jeff MayJeff May

Triggers get a list of objects (up to 200), but process each object individually.  The before trigger as a whole will complete its batch before the after trigger fires.  However, if you have more than 200 objects, there will be multiple before triggers  (1 trigger context for each batch of up to 200 objects).  When each trigger context finishes, the records in that trigger become eligible for after triggers.  In your after triggers, you can't be certain that it is the same set of 200 objects that were in your original before trigger since a user may have created/updated a record which would get included in the trigger batch.

 

As an alternative to triggers, have you thought about using Workflow Rules and Field Updates/Email Alerts?  Those are both non-programming approaches.

Joey HoJoey Ho
Thanks for the explanation! Makes total sense the order of execution. However this means I can't use the COMMON FIELD because it can cause unexpected errors. The reason why I am using Before and After insert triggers is because I am writing apex trigger and classes to handle incoming Email-to-Case updates which than sends out the email updates to Contact of Case, Owner of Case and all Case Team members. This apex trigger also emails any attachments as well which is something we need. The only reason why Before insert was used was because it updated a COMMON FIELD in the related Case record which I could than use in Email Templates meaning I wouldn't have to go into code to change the template format in future. But it looks like I will physically need to hard code the Email text and attachment into Apex instead of using setTemplateId method. Many thanks again.