Sub topic 8
After Triggers: Using the Id and Touching Related Records
This chapter assumes you can write a basic Apex trigger, know what Trigger.new and Trigger.old hold, and understand insert, update, and delete DML. It covers what an after trigger can do that a before trigger cannot.
Where after triggers run
An after trigger runs once the record has been written to the database but not yet committed. The record has a real Id. It is still inside the same transaction, so any error rolls everything back.
Why the Id is available only here
In before insert, the record has not been saved, so Id is null. In after insert, the database has assigned the Id. This makes after triggers the right place to create child records that point to the new record.
trigger AccountFollowUp on Account (after insert) {
List<Task> tasks = new List<Task>();
for (Account a : Trigger.new) {
tasks.add(new Task(Subject = 'Follow up', WhatId = a.Id));
}
insert tasks;
}The trigger builds one list and runs one insert. Putting DML inside the loop would use one DML statement per record and hit governor limits on bulk loads.
Trigger.new is read-only
In an after trigger, Trigger.new is read-only. If you assign a value to a field on a record in it, Salesforce throws an error, because the record is already saved. To change the triggering record after the save, query it again (or build a new sObject with the same Id) and run an update. That update fires triggers again, so plan for it.
trigger AccountAfterUpdateBad on Account (after update) {
for (Account a : Trigger.new) {
a.Description = 'Changed'; // Error: record is read-only
}
}Use a before trigger when you only need to change the record being saved. It needs no DML and costs less.
Touching related records
After triggers are the usual place to update records that depend on the saved one. Examples are creating tasks, updating a parent or children, or writing audit records. Each DML statement here can start more automation on the related object, including its own triggers. All of it shares the same limits for the transaction.
Common mistakes
- Setting fields on
Trigger.newin an after trigger. - Running DML or SOQL inside a
forloop. - Updating the same object from an after update trigger without a guard. A workflow field update or your own DML can make the trigger run again.
A static set keeps its values for the whole transaction, so it can skip records already handled.
public class TriggerGuard {
public static Set<Id> processed = new Set<Id>();
}
trigger AccountAfterUpdate on Account (after update) {
List<Account> toProcess = new List<Account>();
for (Account a : Trigger.new) {
if (!TriggerGuard.processed.contains(a.Id)) {
TriggerGuard.processed.add(a.Id);
toProcess.add(a);
}
}
// Work only with toProcess here
}Exercise
You need to create a related Task for every new Account, and the Task must reference the Account. Which trigger event do you use, and why?
Show answer
Use after insert. The Account's Id does not exist in before insert, but it is assigned once the record is saved, so the Task's WhatId can be set. Collect the Tasks in a list and insert them with one DML statement.
Exercise
An after update trigger sets a field on a record in Trigger.new. What happens, and what should you do instead?
Show answer
Salesforce throws an error because Trigger.new is read-only in after triggers. Move the field change to a before trigger, where no DML is needed. If it must happen after the save, update a separate copy of the record by Id and guard against recursion.