Sub topic 3
How a trigger ends up calling itself
This chapter assumes you can write a simple Apex trigger on a standard object, you know the DML statements (insert, update), and you know that governor limits apply to each transaction. Here you will see the mechanism that makes a trigger run again inside its own transaction.
The core idea
A trigger runs when records are saved. If the trigger saves records itself with DML, that save is a new save, so Salesforce runs the triggers for it. When the DML targets the same object and the same event, the trigger runs again. A trigger that calls itself this way is recursive.
All of these runs happen in one transaction. They share one set of governor limits, and they stop only when the logic stops changing records or Salesforce aborts the transaction.
The classic case: DML in an after trigger
This trigger updates every account it receives. The update fires the same trigger again.
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
}Follow one record through it:
- A user updates Account A.
- The
after updatetrigger runs and callsupdateon Account A. - That
updatefiresafter updateagain, and the trigger callsupdateon Account A again. - Step 3 repeats until Salesforce stops it.
Salesforce allows a trigger chain to nest only so deep. At that point it throws a "maximum trigger depth exceeded" error and rolls back the whole transaction, including the user's original save. The user sees an error on a save that looked harmless.
Other routes back into the same trigger
Direct DML is the obvious cause. Recursion also arrives indirectly, because the order of execution can touch the same records again after your trigger finishes.
- Workflow field updates. If a workflow rule updates a field on the record, Salesforce re-runs the before and after update triggers once for that update. Your trigger then sees the record twice in one transaction.
- Two objects that update each other. An Account trigger updates a Contact, and a Contact trigger updates the parent Account. Each trigger starts the other, so the loop runs across objects.
- Other automation. Flows and other processes that save records in the same transaction can cause the same re-entry.
The workflow case does not loop forever, but it still breaks logic that assumes it runs once per save. A trigger that adds a task or increments a counter will do it twice.
Why `Trigger.new` is not a clue
When the trigger runs a second time, Trigger.new holds records that look like any other update. Nothing in the context tells the trigger that it is a repeat. You need to add that knowledge yourself, which later chapters cover with changed-field checks and static variables.
Reading the symptoms
| Symptom | Likely cause |
|---|---|
| "Maximum trigger depth exceeded" error | Unguarded DML on the same object in a trigger |
| Side effects such as tasks or emails duplicated | Trigger re-fired by a workflow field update |
| Limit errors such as too many DML statements or SOQL queries | Repeated runs sharing one transaction's limits |
What to do next
The fixes follow from the causes. If the trigger only needs to change the records that are being saved, you can often avoid DML altogether. Otherwise, you restrict the logic to real changes, or you remember what has already run. The next chapters take these in turn.
Exercise
An after update trigger on Account calls update on the same Account records to set a field. Why does this cause an error, and what does Salesforce do when it happens?
Show answer
The trigger's own update is a new save, so the after update trigger fires again for the same records, and again after that. Salesforce stops the chain at its maximum trigger depth, throws a "maximum trigger depth exceeded" error, and rolls back the whole transaction, including the original save.
Exercise
A workflow field update on Opportunity causes your before update trigger to run twice for one user save. Does that produce an infinite loop? Why might it still be a problem?
Show answer
It does not loop forever, because the workflow update re-fires the triggers only once. It is still a problem if the trigger logic assumes one run per save, since actions such as creating a task or incrementing a counter happen twice.