Sub topic 7
Branching with isInsert, isBefore and friends
This chapter assumes you can read basic Apex (loops, collections, switch), know what a trigger is, and know the seven trigger events. It covers the Boolean context variables that let one trigger tell which event fired.
Why branch at all
A trigger can list several events, such as before insert, before update, after insert. The same body runs for each one. Often the logic differs per event, so you need a way to ask which event is running. The Boolean context variables answer that.
The context flags
All of these are static members of the Trigger class and return a Boolean, except size, which returns an Integer.
| Variable | True when |
|---|---|
| `Trigger.isExecuting` | The current code is running inside a trigger, not a Visualforce page, API call, or test method called directly |
| `Trigger.isInsert` | The trigger fired because of an insert |
| `Trigger.isUpdate` | The trigger fired because of an update |
| `Trigger.isDelete` | The trigger fired because of a delete |
| `Trigger.isUndelete` | The trigger fired because of an undelete |
| `Trigger.isBefore` | The trigger fired before records were saved |
| `Trigger.isAfter` | The trigger fired after records were saved |
| `Trigger.size` | The total number of records in the call, old and new combined |
Each event sets exactly one operation flag (isInsert, isUpdate, isDelete or isUndelete) and exactly one timing flag (isBefore or isAfter). Combine one of each to identify the event.
A worked example
This trigger covers three events and does different work for each.
trigger ContactTrigger on Contact (before insert, before update, after insert) {
if (Trigger.isBefore) {
for (Contact c : Trigger.new) {
if (String.isBlank(c.Title)) {
c.Title = 'Unknown';
}
}
}
if (Trigger.isAfter && Trigger.isInsert) {
List<Task> tasks = new List<Task>();
for (Contact c : Trigger.new) {
tasks.add(new Task(Subject = 'Welcome call', WhoId = c.Id));
}
insert tasks;
}
}The isBefore block runs for both before insert and before update. It edits Trigger.new directly, so no DML is needed. The isAfter && isInsert block runs only after an insert, when record Ids exist. Test isAfter before using Id values, because a before insert trigger has no Ids yet.
Flags or operationType
Trigger.operationType returns a System.TriggerOperation value such as BEFORE_INSERT. It combines timing and operation in one value, so a switch is often cleaner when you handle many events separately. Flags are shorter when one branch spans several events, as isBefore does above.
Common mistakes
- Using
Trigger.isUpdateand then readingTrigger.oldin an insert.Trigger.oldis null on insert, so check the operation first. - Assuming
Trigger.sizeis the number of changed records. It is the size of the batch, up to 200 records per chunk. - Relying on
Trigger.isExecutingto decide logic. It only says you are in a trigger context, so it rarely helps in a trigger body. It is more useful in a shared class that can be called from both a trigger and other code.
Exercise
A trigger is declared on Account (before insert, before update, after update). Write the condition that is true only for the after update event, and say why Trigger.isUpdate alone is not enough.
Show answer
Use Trigger.isAfter && Trigger.isUpdate. Trigger.isUpdate is true for both before update and after update, so it cannot tell them apart. The timing flag separates them.