Sub topic 4
Prefer before triggers: change records without DML
This chapter assumes you can write a basic Apex trigger, know the difference between before and after events, and understand DML statements such as update. It also assumes you know that governor limits cap what one transaction can do.
The problem with extra DML
A trigger that runs update on the same object it fires on starts another round of trigger execution. If nothing stops that second round from running the same update, the trigger calls itself again and again. Salesforce ends the transaction with a "maximum trigger depth exceeded" error.
Here is the unguarded version:
trigger AccountTrigger on Account (after update) {
List<Account> toUpdate = new List<Account>();
for (Account acc : Trigger.new) {
toUpdate.add(new Account(Id = acc.Id, Description = 'Reviewed'));
}
update toUpdate; // fires this trigger again, with no guard
}The update fires AccountTrigger again, and that run issues the same update. The cycle has no exit.
Why the after trigger needs DML
In an after trigger, the records in Trigger.new are already saved to the database and are read-only. If you want to change a field on them, you have to build new sObject instances and save them with DML. That extra DML is what re-fires the trigger.
A before trigger runs before the records are saved, and the records in Trigger.new can be edited. Salesforce saves whatever values you leave in them. No DML statement is needed, so nothing re-fires.
trigger AccountTrigger on Account (before update) {
for (Account acc : Trigger.new) {
acc.Description = 'Reviewed';
}
}This version has no update statement and cannot recurse. It also uses no DML statements or DML rows from the governor limits.
When a before trigger is not enough
Prefer before for changes to the same record, but know where it stops working:
- Related records. To change a child or parent record, you need DML on that other object. This does not recurse on its own, unless a trigger on that object updates the original one.
- Values that need the record Id on insert. In a
before inserttrigger, new records have noIdyet. If you need to create a related record that points to it, use anafter inserttrigger. - Values set by later steps. Workflow field updates and other automation run after your
beforetrigger. They can change the record again and cause thebeforeandaftertriggers to run a second time.
In these cases you need one of the guards covered in later chapters.
Choosing the event
Ask one question when you design a trigger: does the logic change fields on the record that fired it?
- If yes, use a
beforeevent and set the fields onTrigger.new. - If you only read the saved record or change other objects, use an
afterevent. - If you must change the same object in an
afterevent, add a guard so the second run does nothing.
| Goal | Event | DML needed |
|---|---|---|
| Set a field on the record being saved | before insert / before update | No |
| Create a child record that needs the new Id | after insert | Yes, on the child object |
| Update the same object from an after event | after update | Yes, and it needs a guard |
Exercise
A trigger on Contact runs on after insert and sets Description on each new Contact by building new Contact records and calling update. What change removes the extra DML and the recursion risk, and why does it work?
Show answer
Move the logic to a before insert trigger and set con.Description directly on each record in Trigger.new. In a before event the records have not been saved yet, so Salesforce saves the edited values as part of the original operation. No update statement runs, so the trigger is not fired again.