Sub topic 1
Trigger Events and Context Variables
Prerequisites
This chapter assumes you can read basic Apex, including List, Set, and Map. You should have written a simple trigger on a standard object and know what insert and update do. You should also know that Salesforce enforces governor limits. Later chapters build on the vocabulary introduced here.
Why context matters for recursion
A trigger is Apex code that runs when records are saved. Recursion happens when that code causes the same trigger to run again. To prevent it, you first need to know which event is running and which records the trigger holds. Salesforce gives you both through context variables.
Trigger events
A trigger declares the events it listens to. Each event pairs a timing with a DML operation.
before insert,before update,before delete: run before the record is written to the database.after insert,after update,after delete,after undelete: run after the record is written, but still inside the transaction.
A transaction is one unit of work. If any part fails, Salesforce rolls back everything. All the code in this tutorial runs inside a single transaction.
The difference between before and after matters a lot. In a before trigger, the record has not been saved yet, so you can change its fields directly. In an after trigger, the record has an Id and is saved, but its fields are read-only. To change it, you need another DML statement. That extra DML is the usual cause of recursion.
Context variables
The Trigger class exposes context variables. They tell you what is happening right now.
| Variable | Type | What it holds |
|---|---|---|
| `Trigger.new` | `List<SObject>` | The new versions of the records. Available on insert, update, and undelete. |
| `Trigger.old` | `List<SObject>` | The previous versions. Available on update and delete. |
| `Trigger.newMap` | `Map<Id, SObject>` | New records keyed by Id. Not available on before insert. |
| `Trigger.oldMap` | `Map<Id, SObject>` | Old records keyed by Id. Available on update and delete. |
| `Trigger.isBefore`, `Trigger.isAfter` | `Boolean` | Which timing is running. |
| `Trigger.isInsert`, `Trigger.isUpdate`, `Trigger.isDelete`, `Trigger.isUndelete` | `Boolean` | Which operation is running. |
| `Trigger.size` | `Integer` | The number of records in this invocation. |
Trigger.new is editable only in before triggers. In after triggers, writing to it throws an error.
Reading the context in code
This trigger runs on two events and uses the context variables to branch.
trigger AccountTrigger on Account (before insert, after update) {
if (Trigger.isBefore && Trigger.isInsert) {
for (Account acc : Trigger.new) {
System.debug('Before insert: ' + acc.Name);
}
}
if (Trigger.isAfter && Trigger.isUpdate) {
for (Account acc : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(acc.Id);
System.debug(acc.Name + ' rating: ' + oldAcc.Rating + ' -> ' + acc.Rating);
}
}
}The pairing of Trigger.new with Trigger.oldMap is the basis for the changed-field check you will use later. Comparing the two tells you whether a field really changed.
Chunks of 200
When a bulk operation touches many records, Salesforce does not hand them all to your trigger at once. It splits them into chunks of up to 200 records. Your trigger runs once per chunk, in the same transaction. So Trigger.size is at most 200 in each invocation. This detail is the source of a common bug in recursion guards, covered in a later chapter.
A common mistake
Putting a Trigger.isUpdate check in the wrong place is easy to do. A trigger that listens to after update and writes back to the same object with update re-fires itself, because the new update is also an after update event. Knowing which event you are in is the first step toward stopping this. The next chapters show how.
Exercise
A trigger runs on before update and after update. In which of the two can you assign acc.Description = 'Reviewed' on a record from Trigger.new and have it saved without a DML statement?
Show answer
In before update. The record has not been written yet, so changes to Trigger.new are saved automatically. In after update, the records are read-only, and you would need a separate update statement, which can re-fire the trigger.
Exercise
A user updates 450 Accounts in one operation. How many times does the after update trigger run, and how many records does Trigger.new hold in each run?
Show answer
Three times, with 200, 200, and 50 records. Salesforce splits the operation into chunks of up to 200 records, all within the same transaction.