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
Ron08Ron08 

Trigger.oldmap returning new values

My trigger.oldMap does not seem to return old values if the amount is changed?

Trigger:
trigger TestFunctions on Opportunity (after insert,  after update, after delete, after undelete) {
    if(checkRecursive.runOnce())
    {
    
    if(trigger.isinsert|| trigger.isundelete){
        testFuncClass1.handleInsert(trigger.new);
    }
    if(trigger.isupdate|| trigger.isdelete){
       testFuncClass2.handleupdate(trigger.newmap, trigger.oldmap);
        //system.debug('trigger.oldmap'+trigger.oldmap);
    }
        
    }     
}

Class:
public class testFuncClass2 {
    public static void handleUpdate(map<id, opportunity> updateNewOpps, map<id, opportunity>updateOldOpps){
        if(recusrssionPreventController.flag){

            for(opportunity o : [select id, amount from opportunity where id in : updateNewOpps.keySet()]) {
                system.debug(o);// returns new value
            }  
            for(opportunity o2 : [select id, amount from opportunity where id in : updateOldOpps.keySet()]) {
                system.debug(o2);// returns new value???
            }  
            system.debug(updateNewOpps);//Returns correct values
            system.debug(updateOldOpps);//Returns correct values
            
    }
    }
}

Can anyone please help?
Best Answer chosen by Ron08
Gokula KrishnanGokula Krishnan
Hi Ron,

You no need to Query again, you need to take the values from the map field,

Try this:
Class:

public class testFuncClass2 {
    public static void handleUpdate(map<id, opportunity> updateNewOpps, map<id, opportunity>updateOldOpps){
        if(recusrssionPreventController.flag){
            
            for(opportunity o : updateNewOpps.values()) {
                system.debug('<<<Amount - New Values>>>'+o.amount);// returns new value
                system.debug('<<<Amount - Old Values>>>'+updateOldOpps.get(o.id).amount);// returns old value
               
                //condition to check Amount old and new values
                if(o.amount != updateOldOpps.get(o.id).amount){
                       //code here;
                } 
      
            } 
        }
    }
}

If it helps you, please mark is as best answer, so it will be helpful for other developers.