Salesforce triggers and order of execution

| By Webner

Triggers in Salesforce and order of execution

A trigger is Apex code that executes before or after the following types of operations:
You can create triggers on both standard as well as custom objects.

There are two types of events to start trigger execution:

1. Before triggers are executed before the record is saved in database. They are generally used to update or validate record values before committing to database.

2. After triggers are executed after the record is saved in database. They are generally used to access System set field values which are generated after saving the record in database (such as a record’s Id or LastModifiedDate field), and to make changes in other records, e.g. logging into an audit table or firing asynchronous events with a queue. The records that fires the “after trigger” events are read-only.

Triggers and Orders of execution:

When a record is saved with an insert, update, or upsert statement, following get executed in the given order:

1. All the “before triggers”
2. System validation rules
3. Then record will be saved to the database, but it will not be committed yet
4. All after triggers
5. All assignment rules
6. Auto-response rules
7. All Workflow rules
8. If there will be any workflow field updates, the record will be updated again
9. If the record would be updated with workflow field updates, before and after triggers fire one more time
10. All Escalation rules
11. All DML operations will be committed to the database.
12. Post-commit logic will be executed, such as sending an email.

One comment

Leave a Reply

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