PDI · Question #214
What is the value of the Trigger.old context variable in a before insert trigger?
The correct answer is D. An empty list of sObjects. In a before insert trigger, Trigger.old represents the old versions of records being modified, and since records are being newly inserted, there are no "old" versions, so Trigger.old is an empty list.
Question
What is the value of the Trigger.old context variable in a before insert trigger?
Options
- AA list of newly created sObjects without IDs
- Bnull
- CUndefined
- DAn empty list of sObjects
How the community answered
(64 responses)- A3% (2)
- B6% (4)
- C2% (1)
- D89% (57)
Why each option
In a `before insert` trigger, `Trigger.old` represents the old versions of records being modified, and since records are being newly inserted, there are no "old" versions, so `Trigger.old` is an empty list.
Trigger.new contains newly created sObjects. Trigger.old is for *old* versions.
null is a specific value indicating no object reference. While semantically similar to "nothing," for Trigger.old, it's an empty list, not null.
"Undefined" is not a standard Apex value for collection types in this context. Apex collections will typically be either populated, empty, or explicitly null if uninitialized, but Trigger.old is always initialized as a list.
Trigger.old contains a list of the old versions of the sObject records. In a `before insert` trigger, records are being created for the first time, meaning there are no previous versions of these records in the database. Therefore, `Trigger.old` will always be an empty List<sObject> in a `before insert` context.
Concept tested: Trigger context variables (Trigger.old) in before insert
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
Topics
Community Discussion
No community discussion yet for this question.