Understanding Before vs After Triggers in Salesforce
In Salesforce Apex, triggers run in response to data changes (insert, update, delete, undelete). Choosing between before and after triggers depends on whether you need to modify the same record being processed or perform operations that require the record to be committed (or have an Id).
When to choose a BEFORE trigger
Use a before
trigger when you want to validate or modify field values on the record that fired the trigger, prior to saving to the database. Because before
triggers execute before DML, changes to trigger.new are saved automatically without additional DML.
Common use-cases:
- Set default values or compute fields (e.g., auto-fill a calculated field).
- Run validation logic and add errors to prevent DML (using
addError()
). - Normalize or transform data (e.g., trim strings, format phone numbers).
// Example: before insert trigger to set a default value
trigger AccountBeforeInsert on Account (before insert) {
for (Account a : Trigger.new) {
if (a.Industry == null) {
a.Industry = 'Prospecting';
}
}
}
When to choose an AFTER trigger
Use an after
trigger when you need to work with field values that are set by the database (like the record Id), or when you need to access related records reliably. After
triggers execute after the initial DML operation has completed.
Common use-cases:
- Perform follow-up DML on other objects (e.g., create related records referencing Trigger.new Ids).
- Callouts to external services (via future methods / queueable) — note that synchronous callouts are not allowed directly in triggers.
- Work with roll-up summary records or aggregates that require committed data.
// Example: after insert trigger to create related records
trigger ContactAfterInsert on Contact (after insert) {
List tasks = new List();
for (Contact c : Trigger.new) {
Task t = new Task(Subject = 'Welcome email', WhoId = c.Id, Priority = 'Normal');
tasks.add(t);
}
if (!tasks.isEmpty()) insert tasks;
}
Key guidelines and best practices
- Prefer
before
triggers for changes to the same record to avoid extra DML and reduce governor limits. - Use
after
triggers when you require record Ids or need to modify other objects. - Avoid heavy logic inside triggers—move logic into handler classes (Trigger Handler pattern) for maintainability and testability.
- Be mindful of recursion and bulkify your code: always iterate over Trigger.new and move DML outside loops.
- Understand order of execution (validation rules, before triggers, after triggers, workflow rules, processes, escalation rules) when designing logic to avoid conflicts.
Quick decision cheat-sheet
- If you need to change fields on the same record -> use
before
. - If you need the record Id or to create/update other records -> use
after
. - If you need to block the DML based on validation -> use
before
(and calladdError()
).
Following these principles will help you choose the appropriate trigger event and write efficient, bulkified, and maintainable Apex trigger code.
Leave a Reply