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
Suresh RaghuramSuresh Raghuram 

Issue while passing the oldMap to the Class method while calling from the test class

Hi Community,

I came across the following the situation.

I created an opportunity for the test data and I updated the stage of the opportunity. Expected a value to be changed on the other object.
Sample of my code is as follows.

My issue is in Bold letters. The old map is picking the updated value. where i need to compare old value with new value. but after update the old Map is also takinig new value.
What I am doing wrong.

@isTest
private class opporutnityBusinessLogicTest{

static testmethod void mytestmethod1(){
  Ilist<Account> lstAccount = new list<Account>();
            list<Opportunity> lstOpportunity = new  list<Opportunity>();
            list<PP_OPRTY_PRODT__c> lstOpportunityProd =new list<PP_OPRTY_PRODT__c>();
          
            final string BrokerRecType='Customer';
            schema.describesobjectresult r=account.sobjecttype.getdescribe();
            map<string,schema.recordtypeinfo> m=r.getrecordtypeinfosbyname();
            id brkerRecType= m.get(BrokerRecType).getrecordtypeid();
          
             Integer intNoOfRecord=1;
            for(integer i=0; i < intNoOfRecord; i++){
                account a=new account(Name='Test Customer Account'+i,Broker_Id__c='B99999'+i,PP_ID__c='99999BROKER'+i,Start_Date__c=date.today(),End_Date__c=date.today()+5,recordtypeId=brkerRecType);
                lstAccount.add(a);                
            }
            insert lstAccount;
          
            for(integer i=0;i< intNoOfRecord;i++){
                 Opportunity a=new Opportunity
                                            (CloseDate=date.ValueOf('2015-04-28'),Name='Test Opty ' + i
                                               ,StageName='Lead', Strategy__c='Life Insurance'
                                               ,Sub_Type__c='New', AccountId = lstAccount[0].Id
                                               ,Type='New'                                             
                                            );
                lstOpportunity.add(a);    
            }
            insert lstOpportunity;
          
            for(integer i=0;i< intNoOfRecord;i++){
                PP_OPRTY_PRODT__c oppProd = new PP_OPRTY_PRODT__c(Name='Dental'+i
                                                                        , PRODT_STAGE_NM__c ='Lead'
                                                                        , OPRTY_ID__c=lstOpportunity[0].Id);
                lstOpportunityProd.add(oppProd);
            }
            insert lstOpportunityProd;
                               
            List<Opportunity> opt= [Select id, StageName from Opportunity where Id =:lstOpportunity[0].Id];
         
                              
            List<Opportunity> updateOpp = new List<Opportunity>();
                    
            test.startTest();
            Opportunity oppRecord;
            Id oppRecId;
            for(Opportunity oppRec : opt){
                 oppRecId=oppRec.id;
                 oppRecord=oppRec;
            }
            map<Id,Opportunity> oldMap=new map<Id,Opportunity>();        
            oppMap.put(oppRecId,oppRecord);
             system.debug('*************  Opty Map: '+oldMap);
                 
             if(opt != null){                          
                 for(Opportunity op:opt){                                                      
                     op.StageName = 'Closed Deferred';
                     updateOpp.add(op);                    
                 }              
                update updateOpp;
              
             }
             system.debug('*************  Opty Map: '+oldMap);         
             OpportunityArchitectureBusinessLogic.updateOpportunityProduct(updateOpp, oldMap);
           
            test.stopTest();
}
}
Ankit AroraAnkit Arora
According to me, in this 
map<Id,Opportunity> oldMap=new map<Id,Opportunity>();        
oppMap.put(oppRecId,oppRecord);
You are keeping the reference and once you change it here 
if(opt != null){                          
                 for(Opportunity op:opt){                                                      
                     op.StageName = 'Closed Deferred';
                     updateOpp.add(op);                    
                 }              
                update updateOpp;

It actually change the reference in oldMap too. It's pretty difficult to tell you the answer but worth trying this
map<Id,Opportunity> oldMap=new map<Id,Opportunity>();
for(Opportunity oppRec : opt)
{
    Opportunity oppRecord = new Opportunity;
    oppRecord=oppRec;
    oldMap.put(oppRecord.Id,oppRecord);
}
system.debug('*************  Opty Map: '+oldMap);

Let me know if it is not working, I'll try it at my end.