Sub topic 13
Switching on Trigger.operationType
This chapter assumes you know basic Apex syntax, the seven trigger events, and the difference between before and after triggers. It also assumes you have used Trigger.new and Trigger.old.
Why use operationType
A trigger can list several events. Inside it, you need to know which event fired. You can test the Boolean flags Trigger.isBefore, Trigger.isInsert, and so on. That works, but combining two flags gets wordy: if (Trigger.isBefore && Trigger.isUpdate).
Trigger.operationType returns one value that names the exact event. Its type is the enum System.TriggerOperation. An enum is a type with a fixed set of named values. You can use it in a switch statement, so each event gets its own branch.
The seven values
| Event | TriggerOperation value |
|---|---|
| before insert | `BEFORE_INSERT` |
| before update | `BEFORE_UPDATE` |
| before delete | `BEFORE_DELETE` |
| after insert | `AFTER_INSERT` |
| after update | `AFTER_UPDATE` |
| after delete | `AFTER_DELETE` |
| after undelete | `AFTER_UNDELETE` |
Each value combines the timing and the operation. You don't need two flags to identify an event.
A worked example
This trigger handles three events on Contact. Each when branch matches one value.
trigger ContactTrigger on Contact (before insert, before update, after insert) {
switch on Trigger.operationType {
when BEFORE_INSERT {
System.debug('Before insert: ' + Trigger.new.size() + ' records');
}
when BEFORE_UPDATE {
System.debug('Before update: ' + Trigger.new.size() + ' records');
}
when AFTER_INSERT {
System.debug('After insert: ' + Trigger.new.size() + ' records');
}
when else {
System.debug('Unhandled operation: ' + Trigger.operationType);
}
}
}Notice three details:
- The
whenlabels are the bare enum names. You don't writeSystem.TriggerOperation.BEFORE_INSERT. when elsecatches any value without its own branch. If you later addafter updateto the trigger's event list and forget a branch, this fallback shows it in the debug log.Trigger.newis safe in all three branches because none of them is a delete event.
You can also list several values in one branch, separated by commas. This is useful when two events share logic:
trigger LeadTrigger on Lead (before insert, before update) {
switch on Trigger.operationType {
when BEFORE_INSERT, BEFORE_UPDATE {
for (Lead l : Trigger.new) {
if (String.isBlank(l.Company)) {
l.Company = 'Unknown';
}
}
}
}
}Flags versus operationType
Both approaches are valid. Choose by readability:
- Use the flags for a simple split, such as
if (Trigger.isBefore)versusif (Trigger.isAfter). - Use
operationTypewhen you handle three or more distinct events, or when you pass the event to a handler class.
Common errors
- Reading
Trigger.newin a delete branch.Trigger.newis null for delete events. UseTrigger.oldinBEFORE_DELETEandAFTER_DELETE. - Omitting
when else. The code still compiles and runs, but an unexpected event passes silently. - Using
Trigger.operationTypeoutside a trigger. Outside trigger context the value is null. Aswitchon a null enum value runs thewhen elsebranch if one exists. Without one, it does nothing.
Exercise
A trigger on Account is declared for before delete and after undelete. In a switch on Trigger.operationType, which two when labels do you write, and which collection do you read in the first one?
Show answer
Write when BEFORE_DELETE and when AFTER_UNDELETE. In the BEFORE_DELETE branch, read Trigger.old, because Trigger.new is null for delete events.
Exercise
What happens in the ContactTrigger example if you add "after update" to the event list but add no matching when branch?
Show answer
The AFTERUPDATE value falls into when else, so the trigger writes the "Unhandled operation: AFTERUPDATE" debug message and does nothing else.