You need to sign in to do that
Don't have an account?
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!
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.