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
Vadivel MuruganVadivel Murugan 

Date

My old value does not come, only display new map value why. how can i get old map value in apex trigger
KaranrajKaranraj
Can you share your code here? so ithat it will be easy for us to assist you
Note that old map is only available in update and delete triggers.
JayantJayant
Suppose your field name is My_Date__c and SObject is Account then you need to do this - 

for(Account a: trigger.new) {
 if(a.My_Date__c != trigger.oldMap.get(a.Id).My_Date__c) {
  //do something here
 }
}

Since trigger.old and trigger.oldMap are null for Insert and Undelete triggers, this should only work for Updates and Deletes.

------------------------
If this resolves your problem, please mark the question as Resolved.
Vadivel MuruganVadivel Murugan
public void OnAfterUpdate(Map<id,Visits__c> VisitsNewMap,Map<id,Visits__c> VisitsOldMap){
        if(RecursiveHandler.runOnce1()){
                      
            CheckRevisiedVisitDate(VisitsNewMap,VisitsOldMap); 
            CheckManagerApproval(VisitsNewMap);
                }        
    }

private void CheckRevisiedVisitDate(Map<id,Visits__c> VisitsNewMap,Map<id,Visits__c> VisitsOldMap)
    {
        
        
        Visits__c previsit=new Visits__c();
        Visits__c changevisit=new Visits__c();
        Visits__c visilist=new Visits__c();
        Visits__c managerapproval;     
       for(Visits__c result: VisitsOldMap.values())
        {
        previsit.Actual_Service_Date__c=result.Actual_Service_Date__c;
        }
        for(Visits__c result: VisitsNewMap.values())
        {
        visilist.id=result.id;
        changevisit.Actual_Service_Date__c=result.Actual_Service_Date__c;
        managerapproval = [select id,name,Revised_Actual_Visit__c from Visits__c where id =:visilist.id];
        }
        if(visilist.id!=null)
        {
            if(previsit.Actual_Service_Date__c.month()==previsit.Actual_Service_Date__c.month() && managerapproval.Revised_Actual_Visit__c!=true){
                  for(Visits__c visit: VisitsNewMap.values()){
                    visit.addError('Manager can only reschedule the actual visit in different month');
                    }
             }
         }    
        system.debug('>>>>>>>>>>>month>>>>>>>>>'+previsit.Actual_Service_Date__c+changevisit.Actual_Service_Date__c);
    }     

/*====================================================
    *Method Name     : CheckManagerApproval
    *Description     : Check Manager Approval for Actual Visit Date
    *Arguments       : new Visit list
    ====================================================*/ 
    private void CheckManagerApproval(Map<id,Visits__c> VisitsNewMap)
    {
    Visits__c check;
         for(Visits__c result: VisitsNewMap.values()){
         check = [select id,name,Revised_Actual_Visit__c from Visits__c where id =:result.id];
         check.Revised_Actual_Visit__c=false;
         }
         update check;
    }

Upadte me....
surasura
trigger,Old is  not available for insert make sure your code is running on update . before all that make sure you trigger is defined to run on update 

eg:  
trigger HelloWorldTrigger on Book__c (before update) {

 
Vadivel MuruganVadivel Murugan
I can call my trigger function in only afetr update field
JayantJayant
Which ever method where you want to do this check, should receive trigger.oldMap and trigger.new/newMap as arguments.
In your case that should become - 

Suppose its the method - CheckRevisiedVisitDate(VisitsNewMap,VisitsOldMap) and field Visit_Date_c

then you need to do - 

for(Visit__c v : VisitsNewMap.Values()) {
 if(v.Visit_Date__c != VisitsOldMap.get(v.Id).Visit_Date__c) {
  //your code here
 }
}