Sub topic 4
What the Trigger Class Gives You
This chapter assumes you can read basic Apex (variables, loops, List, Set and Map), know what a record Id is, and have seen DML and SOQL. It also assumes you know that governor limits cap what one transaction can do.
Why context variables exist
A trigger runs in response to a database event, but its body has no parameters. The system gives it information through the built-in Trigger class. Its static properties describe the records involved and the event that fired the trigger. Without them, a trigger could not know which records changed or whether they were inserted or deleted.
The properties fall into three groups.
| Group | Members | Answers the question |
|---|---|---|
| Record collections | `Trigger.new`, `Trigger.old`, `Trigger.newMap`, `Trigger.oldMap` | Which records, and what were their values? |
| Boolean flags | `Trigger.isInsert`, `isUpdate`, `isDelete`, `isUndelete`, `isBefore`, `isAfter`, `isExecuting` | Which event and timing fired? |
| Event and size | `Trigger.operationType`, `Trigger.size` | Which event as an enum value, and how many records? |
The record collections
Trigger.new is a list of the new versions of the records. Trigger.old is a list of the old versions. Trigger.newMap and Trigger.oldMap hold the same records as maps keyed by record Id. The next chapter covers them in detail. For now, remember that they are not all populated for every event. For example, a delete has no new version, so Trigger.new is null there.
The flags and size
The Boolean flags let one trigger handle several events:
Trigger.isInsert,Trigger.isUpdate,Trigger.isDeleteandTrigger.isUndeleteare true for the matching DML operation.Trigger.isBeforeandTrigger.isAftertell you the timing.Trigger.isExecutingis true when the current code is running inside a trigger. It is rarely needed.Trigger.sizeis the total number of records in the call, old and new together. A batch can hold up to 200 records.
Trigger.operationType returns a System.TriggerOperation enum value such as BEFORE_INSERT or AFTER_DELETE. It combines the timing and the event in one value.
A trigger that reads its context
This trigger logs which event fired. It uses the flags and Trigger.size.
trigger ContactContextLogger on Contact (before insert, before update, after insert) {
String timing = Trigger.isBefore ? 'before' : 'after';
String event = Trigger.isInsert ? 'insert' : 'update';
System.debug('Fired: ' + timing + ' ' + event);
System.debug('Records in this batch: ' + Trigger.size);
}The same logic with the enum is covered in a later chapter.
Common errors
- Null pointer on delete. Looping over
Trigger.newin a delete trigger throws aNullPointerException. UseTrigger.oldinstead. - Reading old values on insert.
Trigger.oldandTrigger.oldMapare null on insert, because no earlier version exists. - Assuming one record.
Trigger.newcan hold up to 200 records. Always loop over it. - Assigning to the collections.
Trigger.newandTrigger.oldare read-only as collections. You cannot replace them. In a before trigger you can change field values on the records inside them.
How the pieces fit
Exercise
A trigger on Account is defined for before insert and before delete. Its body loops over Trigger.new. What happens on a delete, and which variable should the loop use?
Show answer
On a delete, Trigger.new is null, so the loop throws a NullPointerException. The loop should use Trigger.old for delete events. Check Trigger.isDelete, or Trigger.operationType, to choose the right collection.
Exercise
What does Trigger.size return for a trigger fired by an update of 150 records?
Show answer
It returns 150, the number of records in the call. Trigger.size counts the records in the batch, and a single batch holds at most 200 records.