function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek KhoslaAbhishek Khosla 

trigger.oldmap and Trigger.newmap

Dear All,

I am new to SFDC. I have been gudied that it is a best practicse to write an apex class and then call that class in a trigger. I want to udersatnd how we can use trigger.oldmap or trigger.newmap in an Apex class. Can anyone help me with an example or provide me a link 
Karan KeharKaran Kehar
Hi Abhishek,

You create a trigger handler class and have a method in that which accepts two maps as method arguments.From trigger you call this handler method and pass trigger.oldmap and trigger.newmap to the method. You can compare the old and new version of the record using this.

You can check the blog below for this
https://blog.mirketa.com/salesforce-developer-guide-use-of-helperhandler-class-to-manage-trigger-execution/
SecondID PatelSecondID Patel
Hi Abhishek,

Here is an example. The Scenario of the trigger is that there is a checkbox on Account object called "Create Contact". If this is changed from unchecked to checked on Insert/Update events then contact is created.
 
Trigger Code

trigger CreateContact on Account (after INSERT, after Update) {
    
   CreateContactHandler.createContactMethod(Trigger.NewMap,Trigger.OldMap);

}

TriggerHandler

public class CreateContactHandler {
    
    public static void createContactMethod(Map<Id,Account> newaccMap, Map<Id,Account> oldaccMap){
        Set<Id> accIds = New Set<Id>();
        List<Contact> conList = New List<Contact>();
        
        for(Account acc : newaccMap.values()){
            if(newaccMap.get(acc.Id).Create_Contact__c && !oldaccMap.get(acc.Id).Create_Contact__c){
              Contact con = New Contact();
              con.AccountId = newaccMap.get(acc.Id).Id;
              con.FirstName =  'FirstName';
              con.LastName =   'LastName' ;
              conList.add(con);  
            }
        }
          INSERT conList;
    }

}
Trigger.OldMap and Trigger.NewMap is useful when you want to check the condition for old values and new values of the fields. For example, in the above example, contact should be created only when Create Contact checkbox is changed from Unchecked to Checked...

Mark, it as best answers if it helps you.

 
Abhishek KhoslaAbhishek Khosla
@secondID patel and @Karankher 

Thanks a lot for sharing. it is really helpful can you please provide me any source where i can learn such concepts and praticse the same. My aim is to write a trigger everyday and partisce as much as I can
Ajay K DubediAjay K Dubedi
Hi Abhishek,

Trigger.newMap - A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.

Trigger.oldMap - A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.
Follow this link for more info:http://www.sfdc99.com/2014/02/25/comparing-old-and-new-values-in-a-trigger/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi