Development questions
What is the difference between Trigger.new and Trigger.old?
Trigger.new is a list of the new versions of records (the values being saved). Trigger.old is a list of the old versions (values before the change).
- Insert: only
Trigger.new - Update: both; compare to detect field changes
- Delete: only
Trigger.old - Undelete: only
Trigger.new
Trigger.new is editable in before triggers; Trigger.old is always read-only.
apex
for (Account a : Trigger.new) {
Account oldA = Trigger.oldMap.get(a.Id);
if (a.Phone != oldA.Phone) { /* phone changed */ }
}
apex-triggertriggercontext-variables
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?
- Why is Trigger.newMap not available in before insert?