Before and After Triggers
By the end of this lesson, you'll be able to:
- Distinguish a before trigger from an after trigger
- Choose the correct trigger timing for a given task
- Explain why modifying a field only works correctly in a before trigger
Prerequisites: "What Is a Trigger?"
before: modify the record on its way in
trigger AccountTrigger on Account (before insert) {
for (Account acc : Trigger.new) {
if (String.isBlank(acc.Industry)) {
acc.Industry = 'Unknown';
}
}
}
trigger AccountTrigger on Account (before insert) {— abefore inserttrigger runs before the record is actually saved — this is exactly Module 19's "What Really Happens When You Insert a Record" sequence, step 2.for (Account acc : Trigger.new) {— loops over the records about to be inserted.if (String.isBlank(acc.Industry)) {— checks for a blank field.acc.Industry = 'Unknown';— changing a field here becomes part of what actually gets saved, with no extraupdateDML needed.}— closes theif.}— closes the loop.}— closes the trigger.
after: react once the record is permanent
trigger AccountTrigger on Account (after insert) {
for (Account acc : Trigger.new) {
System.debug('Account saved with Id: ' + acc.Id);
}
}
trigger AccountTrigger on Account (after insert) {— anafter inserttrigger runs after the save — step 5 from Module 19's sequence.for (Account acc : Trigger.new) {— loops over the now-saved records.System.debug('Account saved with Id: ' + acc.Id);—acc.Idis already populated here — the right place for logic that needs the final saved state.}— closes the loop.}— closes the trigger.
Why field changes belong in before, not after
// WRONG PLACE for a field default
trigger AccountTrigger on Account (after insert) {
for (Account acc : Trigger.new) {
acc.Industry = 'Unknown'; // too late — already saved without this!
update acc; // requires an extra, avoidable DML statement
}
}
// WRONG PLACE for a field default— the label says it all.trigger AccountTrigger on Account (after insert) {— by the time anaftertrigger runs, the record is already saved.for (Account acc : Trigger.new) {— loops over the saved records.acc.Industry = 'Unknown';— changing a field here doesn't retroactively change what was saved.update acc;— requires an entirely separateupdateDML statement to apply the change.}— closes the loop.}— closes the trigger.
Warning: Line 5 runs update acc; inside a for loop — exactly the DML-in-a-loop governor-limit problem from Module 7 and Module 18, on top of being the wrong trigger timing. Defaulting Industry in before insert (previous section) avoids both problems at once: the change becomes part of the original save, with no second DML operation needed at all.
Exercise
As a comment, decide whether defaulting a blank Rating field to 'Cold' belongs in a before or after trigger, and explain why.
Show hint
Think about whether an extra DML statement would be needed.
Before and After Triggers Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
before triggers run prior to the actual save and can still modify the record being saved; after triggers run once the save is complete and can see the final, permanent state — including the Id.