This trigger logs changes to an Account’s Phone field in its Description. It displays the old and new phone numbers, maintaining an audit trail.
Explore more examples via this YouTube playlist.
Solution:
trigger AccountTrigger on Account (before update) {
if(Trigger.isUpdate){ if(Trigger.isBefore){
AccountTriggerHandler.updateDescription(Trigger.New, Trigger.oldMap);
}
}
}
public class AccountTriggerHandler {
public static void updateDescription(List<Account> accList,Map<Id,Account> oldMap){
for(Account acc:accList){
if(acc.Phone!=oldMap.get(acc.Id).Phone){
acc.Description=’Phone is updated! Old Value :
‘+oldMap.get(acc.Id).Phone+’ & New Value :
‘+acc.Phone;
}
}
}
}