Skip to content
  • linkedIn
  • twitter
  • About Us
  • News letter
  • Privacy Policy
  • Contact Us
sfdcgenius

sfdcGenius

Learn Practice and Execute

salesforce
  • Home
  • Blog
  • Apex
  • Toggle search form
How to check whether record values are changed or not in Apex Trigger

How to check whether record values are changed or not in Apex Triggers

Posted on April 2, 2022January 19, 2023 By sfdcGenius No Comments on How to check whether record values are changed or not in Apex Triggers

How To Compare Old Values And New Values in Salesforce Triggers. (How to check whether record values are changed or not in Apex Triggers).
We understand that salesforce Before update and After Update triggers fired/invoked based on Update DML event occurs. Regardless of any field value updated on record.

Trigger.OldMap contains an older version (last version of record committed in the database) of record in map with key as their Salesforce records Id’s.
Trigger.OldMap = Map<Id, OldVersionOfRecord>();

What is Apex Triggers in Salesforce ?

If we need to recognize whether or not the respective field value modified or not. We need to use trigger.new and trigger.oldMap context variables. As we know that trigger.New consists of a list of records so that it will have updated field values and trigger.OldMap consists of a map of records that will have old values.

Example: When email value will change on contact, then only update description field like. Check whether record values are changed or not in Apex Triggers.

trigger Helper Class

 public class Contact_Helper {  
  public static void checkEmailValueUpdated(List<Contact> newList, Map<Id,Contact> oldMap) {  
    for(Contact conRecord: newList){  
    if(conRecord.phone != oldMap.get(conRecord.Id).phone){  
      conRecord.description = 'Email got updated from'+oldMap.get(conRecord.Id).email+' to '+conRecord.email;  
     }  
    }  
  }  
 }  

Trigger:

 Trigger ContactTrigger on Contact(Before update){  
   if(trigger.isBefore && trigger.isUpdate){  
     Contact_Helper.checkEmailValueUpdated(trigger.new, trigger.oldMap);  
   }  
 }  

Notes:

  • Trigger.oldMap is only available in update and delete events.
  • Trigger.oldMap is not available in insert event triggers. Please take care of this factor while writing your apex trigger code.

For more details refer to the official  Apex Triggers link.


Related Post

Apex, Apex Class, Apex Triggers, Customization Tags:How to check whether record values are changed or not in Apex Trigger, Trigger.NewMap Vs Trigger.oldMap, trigger.old vs Trigger.New

Post navigation

Previous Post: What is Apex Triggers in Salesforce ?
Next Post: Write a trigger to count the number of related child records on the parent.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Admin
  • Apex
  • Apex Class
  • Apex Triggers
  • Customization
  • Flow
  • Lightning Web Component
  • Ligthing
  • LWC
  • Recent Post
  • Salesforce Security

Tags

@api @track @wire Admin Apex Apex Class Apex Trigger api connectedCallback() decorators disconnectedCallback disconnectedCallback() errorCallback errorCallback() Flow How to check whether record values are changed or not in Apex Trigger how to create modal in lwc how to create popup in lwc lighting flow LightningAlert LightningConfirm LightningPrompt lightning web component lwc modal/popup in lwc Modal in lwc notification in lwc popup in lwc renderedCallback() Role Hierarchies Salesforce Salesforce Customization salesforce flow salesforce latest update salesforce layoff sfdc sfdcGenius sfdcginus track trailhead Trigger.NewMap Vs Trigger.oldMap trigger.old vs Trigger.New wire wrapper class in apex wrapper class in salesforce

Resources

  • About Us
  • Blog
  • Contact Us
  • Home
  • Newsletter
  • Privacy Policy

Social Media

  • linkedIn
  • twitter

Copyright © 2025 sfdcGenius.

Powered by PressBook WordPress theme