- Published on
When Does an Apex Trigger Fire?
- Authors
- Name
- Tony Geiser
An Apex trigger in Salesforce is a tool to perform custom actions before or after changes to Salesforce records. Triggers are essential in automating processes and ensuring data integrity. Here’s a detailed overview of when Apex triggers fire.
Trigger Events
Before Insert
- Fires before a new record is inserted into the database.
- Useful for validating or modifying the record's values before they are saved.
After Insert
- Fires after a new record has been inserted into the database.
- Commonly used for actions that need to occur after the record is committed to the database, such as creating related records.
Before Update
- Fires before an existing record is updated in the database.
- Useful for validating or modifying record values before they are updated.
After Update
- Fires after an existing record has been updated in the database.
- Often used for post-update processes such as updating related records or logging changes.
Before Delete
- Fires before an existing record is deleted from the database.
- Useful for validation checks to prevent the deletion of certain records or to cascade delete related records.
After Delete
- Fires after an existing record has been deleted from the database.
- Typically used for actions that need to occur after the record is removed, such as removing related records or sending notifications.
After Undelete
- Fires after a record is restored from the Recycle Bin.
- Useful for re-establishing relationships or re-triggering processes for records that were previously deleted.
Execution Context
Apex triggers can operate in the following contexts based on the DML operations being performed:
- Insert: When new records are being added.
- Update: When existing records are being modified.
- Delete: When records are being removed.
- Undelete: When records are being restored.
Order of Execution
- Load record data from the database or initialize new record values.
- Execute system validation rules.
- Run
before
triggers. - Apply duplicate rules.
- Save the record to the database (but not commit yet).
- Run
after
triggers. - Run assignment rules (if applicable).
- Run auto-response rules (if applicable).
- Run workflow rules.
- Execute processes and flows.
- Update the record again if there were changes from the workflow.
- Run
before
update triggers if there were updates in step 11. - Save the record again (but not commit yet).
- Run
after
update triggers if there were updates in step 11. - Execute escalation rules (if applicable).
- Run entitlement rules (if applicable).
- Commit the transaction to the database.
- **Execute post-commit logic (such as sending email).
Practical Examples
- Before Insert: Automatically set a default value for a field.
- After Insert: Send a welcome email to a new user.
- Before Update: Prevent a change to a record if it does not meet specific criteria.
- After Update: Update a related record with the latest information.
- Before Delete: Check if a record can be safely deleted based on related records.
- After Delete: Log the deletion of records for audit purposes.
- After Undelete: Restore relationships or status of records that were previously deleted.
Best Practices
- Keep Logic Out of Triggers: Use triggers to initiate logic contained in helper classes rather than placing logic directly within the trigger.
- Avoid Recursive Triggers: Ensure that triggers do not call themselves repeatedly, which can lead to infinite loops and exceed governor limits.
- Bulkify Your Code: Write triggers that can handle bulk operations efficiently.
- Use Context Variables: Leverage
Trigger.isInsert
,Trigger.isUpdate
, etc., to control the flow based on the context of the operation.
Understanding when and how Apex triggers fire is crucial for effective Salesforce development and ensuring seamless automation within your Salesforce environment.
As always, please refer to the official Salesforce documentation here.