Development questions
When an Account Phone is updated, put the old phone number and new phone number into the Account Description field.
Use before update so the Description is saved without an extra DML.
apex
trigger AccountTrigger on Account (before update) {
for (Account a : Trigger.new) {
Account old = Trigger.oldMap.get(a.Id);
if (a.Phone != old.Phone) {
a.Description = 'Old Phone: ' + old.Phone + ', New Phone: ' + a.Phone;
}
}
}
apex-triggertriggercodingscenario
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).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?