How To Compare Old Values And New Values In Salesforce Triggers

Written By Venu Gutta Salesforce LWC on Monday, 2 December 2019 | 23:36

How To Compare Old Values And New Values In Salesforce Triggers (How to check whether record values are changed or not in Apex Trigger)

We know that salesforce Before update and After Update triggers fired/invoked based on Update DML event occurs, irrespective of any field value updated on record. But update triggers should not get invoked every time when record is updated. These trigger's should fire when corresponding field values got changed on record.

If we want to know whether respective field value changed or not, we need to use and trigger.oldMap context variables. As we know that trigger.New contains list of records which will have updated field values and trigger.OldMap contains map of records which will have old values. Lets take basic example: If Phone field value on Account got changed, then update Description field like "Phone number got updated from old Phone Number to New Number. Here the logic should not trigger every time, only gets triggered when only phone field value got updated.

Trigger Helper Class

public class Account_Helper {
  public static void checkPhoneValueUpdated(List<account> newList, Map<Id,Account>  oldMap) {
     for(Account accountRecord: newList){
      if( != oldMap.get(accountRecord.Id).phone){
          accountRecord.description = 'Phone number got updated from'+oldMap.get(accountRecord.Id).phone+' to ';
Trigger AccountTrigger on Account(Before  update){
    if(trigger.isBefore &amp;&amp; trigger.isUpdate){
        Account_Helper.checkPhoneValueUpdated(, trigger.oldMap);


Post a comment