Order of Execution of Trigger in Salesforce

By | December 16, 2019

Hi guys, here we are going to learn about order of execution of triggers in salesforce.

  1. Original record is loaded from the database.
  2. New record overwrites old values.
  3. System validation rules.
  4. All Apex before triggers.
  5. Custom validation rules.
  6. Record saved to database. [but not committed]
  7. Record reloaded from database.
  8. All Apex after triggers.
  9. Assignment rules.
  10. Auto Response rules
  11. Workflow rules.
  12. Process
  13. Escalation rules.
  14. Parent Roll-up summary formula value updated. (if parent)
  15. Database commit.
  16. Post-commit logic (sending emails).

STEP 1:  System validation rules: – This includes…

  • Verifying that required fields have values.
  • Valid field formats.
  • Maximum field length.
  • Foreign keys are valid.

Additionally, if the save operation has been called from a standard UI, it will be checked:

  • Compliance with layout-specific rules
  • Required values at the layout level

Note: –If system validations fail at this stage, next steps will never be executed. 

STEP 2:  All Apex before triggers: –If an error occurs in a before trigger, the following steps won’t be performed.

  • If there are several before triggers, the order of execution of them in not guaranteed.
  • Before triggers is the correct place to derive field values, as the record can still be edited.
  • Before triggers is not the correct place to perform Apex validations, as any other before triggers could execute after yours and change the record, skipping your validations. This problem is known as the evil trigger problem.

STEP 3:  System Validation Rules are executed again, except layout specific rules. This means that if my before trigger did something that left the record in a non-valid state, for example emptying a required field, at this point the execution will stop, because it will be re-validated.

Note: –System validations validate changes performed in before trigger. If system validations fail at this stage, next steps will never be executed.

STEP 4: Custom Validation Rules: – Are executed. Custom validation rules are the rules that you can define declaratively in the platform, that will throw an error if certain conditions are meet on record save.

Note: –Custom validation rules validate changes performed in before trigger. If custom validations fail, next steps will never be executed.

STEP 5: Record is saved to database (but not committed).

STEP 6: All Apex after triggers: – execute. At this point the record is not editable anymore, as it was in before triggers.

  • Again, the execution order of after triggers if there are several in your org is not guaranteed.
  • This is the correct place to perform Apex validations, as the record can’t be changed anymore and evil trigger problem won’t happen.

Note: –If a validation is thrown in an after trigger, next steps, as workflow rules, will never be executed.

STEP 7:  Assignment rules: – are executed. Assignment rules are rules that you can assign Leads and Cases to a specific user or queue based in certain criteria when they are created.

STEP 8: Auto-response rules: – are executed. Auto-Response Rules allow you to generate an email automatically in response to an incoming Lead or Case.

STEP 9: Workflow rules: – are executed. Workflow rules is one of the ways we have in the platform to automate processes declaratively. With workflow rules you can do the following:

  • Assign a new task to a user, role, or record owner.
  • Send an email to one or more recipients you specify.
  • Update the value of a field on the record that is being saved, or in a related object.
  • Send a secure, configurable API message (in XML format) to a designated listener (outbound message).

Note: –As a workflow rule can update the record that has already been saved, if this situation happens, system validations, before and after triggers are executed again. However custom validation rules are not executed again.

STEP 10:  Processes: -Executes processes and flows launched via processes and flow trigger workflow actions.

When a process or flow executes a DML operation, the affected record goes through the save procedure.

Note: –

  • As process can update the record that has already been saved, if this situation happens, system validations, custom validation rules, before and after triggers are executed again.
  • This means that my trigger can be executed three times: the initial one, after a workflow rule has been executed, and after a process has been executed. 

STEP 11: Escalation rules are executed. These are rules for automatically escalating cases when a certain criteria is meet.

STEP 12: Parent Rollup Summary Formula value are updated (if present):

  • If the record contains a roll-up summary, then it performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.

STEP 13Data is committed to the database.

STEP 14: All post-commit logic is executed, for example sending emails, etc.

Schema to help understanding what is happening behind the scenes

Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

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