Sub topic 3
Before versus after: when each runs and why it matters
This chapter assumes you can write basic Apex, know what a record Id is, and have used DML and SOQL. It covers how the two trigger timings differ and how to choose between them.
Where the two timings sit
Salesforce runs a before trigger before it writes the record to the database. It runs an after trigger once the record has been written, but before the transaction commits. The diagram shows a simplified view of the save.
This ordering explains every difference between the two timings.
What a before trigger can do
The records in Trigger.new have not been saved yet, so you can edit their fields directly. Salesforce saves your edits along with the record. You do not need a DML statement.
trigger AccountTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (String.isBlank(a.Description)) {
a.Description = 'Set by AccountTrigger';
}
}
}In a before insert trigger, the record has no Id yet, so a.Id is null. This makes before triggers a poor place to create child records that need the parent's Id.
Before triggers are also the right place for validation, because rejecting a record here costs less than rolling it back later.
What an after trigger can do
In an after trigger, the record exists and has an Id. Trigger.new is now read-only. Assigning to a field on a record in it fails at runtime with a "Record is read-only" error.
An after trigger is the place to create or update other records that refer to the saved one.
trigger AccountFollowUp on Account (after insert) {
List<Task> tasks = new List<Task>();
for (Account a : Trigger.new) {
tasks.add(new Task(
Subject = 'Follow up with new account',
WhatId = a.Id
));
}
insert tasks;
}Notice that the DML statement sits outside the loop. Collecting records in a list and inserting once keeps you within governor limits.
If you must change fields on the triggering records themselves from an after trigger, you have to query fresh copies and run DML on them. That DML fires the triggers again, which is why recursion becomes a concern. A before trigger avoids this extra save.
Choosing between them
| Requirement | Use | Reason |
|---|---|---|
| Set or default a field on the same record | Before | Edits are saved with no extra DML |
| Validate and reject a record | Before | Fails early, before the write |
| Create child or related records | After | The Id exists |
| Read values calculated by the system | After | Saved values are final |
| Update other objects | After | The triggering record is saved first |
Common errors
- Reading
Idin a before insert trigger and getting null. - Assigning a field in an after trigger and hitting the read-only error.
- Updating the same object from an after update trigger without a recursion guard.
- Putting related-record creation in a before trigger, where the parent Id is not available.
Exercise
A requirement says that when a Contact is inserted, its Description must default to "New contact", and a Task must be created for it. Which timings do you use for each part, and why?
Show answer
Use a before insert trigger for the Description, because you can set the field on Trigger.new with no DML. Use an after insert trigger for the Task, because the Contact's Id exists only after the save, and the Task needs it in WhatId or WhoId.