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
Sanu VermaSanu Verma 

Not able to update Opportunity field Test Class

the problem is i am not able to update opportunity field meanss their is class which have some condtion. i write a test class to cover that class.
public with sharing class Sample{

    public static void lastThreeMonthsVolumeOnLead(List<Opportunity> opps, 
                                                    Map<Id, Opportunity> oldOpps){
        
        Map<String, BankVolumeInfoOnLeadWrapper> appIdWithBankInformation = 
                new Map<String, BankVolumeInfoOnLeadWrapper>();
		System.debug(opps);
        System.debug(oldOpps);                                                
        for(Opportunity opp : opps){
            //this condition which i want to make fullfill in test class
            if(opp.Volume_Last_Month__c != oldOpps.get(opp.Id).Volume_Last_Month__c || 
                    opp.Volume_Two_Months_Ago__c != oldOpps.get(opp.Id).Volume_Two_Months_Ago__c || 
                    opp.Volume_Three_Months_Ago__c != oldOpps.get(opp.Id).Volume_Three_Months_Ago__c ){
                System.debug('hello i m here');
                BankVolumeInfoOnLeadWrapper bankVolumeWrapper = new BankVolumeInfoOnLeadWrapper();
                bankVolumeWrapper.volumeLastMonth = opp.Volume_Last_Month__c;
                bankVolumeWrapper.volumeTwoMonthsAgo = opp.Volume_Two_Months_Ago__c;
                bankVolumeWrapper.volumeThreeMonthsAgo = opp.Volume_Three_Months_Ago__c;   

                appIdWithBankInformation.put(opp.Id, bankVolumeWrapper);
            }
        } 

        if(appIdWithBankInformation.size() != 0) updateLeadWithBankVolumes(appIdWithBankInformation);  
    }

    static void updateLeadWithBankVolumes(Map<String, BankVolumeInfoOnLeadWrapper> 
                                                appIdWithBankInformation){

        Map<Id, Lead> leadsToUpdate = new Map<Id, Lead>();

        for(Lead lead : [SELECT Id, Volume_Last_Month__c,
                                Volume_Two_Months_Ago__c, 
                                Volume_Three_Months_Ago__c, 
                                Renewal_App__c
                                FROM Lead WHERE IsConverted = false AND 
                                Renewal_App__c != null AND 
                                Renewal_App__c IN: appIdWithBankInformation.keySet()]){
            
            BankVolumeInfoOnLeadWrapper bankVolumeWrapper = 
                appIdWithBankInformation.get(lead.Renewal_App__c);
            
            lead.Volume_Last_Month__c = bankVolumeWrapper.volumeLastMonth;
            lead.Volume_Two_Months_Ago__c = bankVolumeWrapper.volumeTwoMonthsAgo;
            lead.Volume_Three_Months_Ago__c = bankVolumeWrapper.volumeThreeMonthsAgo;
            
            leadsToUpdate.put(lead.Id, lead);
        }

        if(leadsToUpdate.size() != 0) update leadsToUpdate.values();
    }

    public class BankVolumeInfoOnLeadWrapper{
        public Decimal volumeLastMonth = 0;
        public Decimal volumeTwoMonthsAgo = 0;
        public Decimal volumeThreeMonthsAgo = 0;
        public bankVolumeInfoOnLeadWrapper(){}
    }

}
In above code i made bold tht condition which i want to full fill in test class.

and here is my test class
@isTest
public class SampleTest{
    
    @isTest
    static void getlastThreeMonthsVolumeOnLead()
    {
        Test.startTest();
         insert UtilityTest.getTriggerExecutionCS();
       
        Account acc = UtilityTest.getAccountWithRecordLabel('Broker Account');
        insert acc;
        
        Account acc1=UtilityTest.getAccountWithRecordLabel('Merchant Accounts');
        acc1.Name= 'Tiger Funding';
        insert acc1;
        
        Opportunity opp=UtilityTest.getOpportunity(acc.Id);
        opp.Volume_Last_Month__c=5;
        opp.Volume_Two_Months_Ago__c=25;
        opp.Volume_Three_Months_Ago__c =20;
        opp.Name='Test1';
        insert opp;
        System.debug(opp);
        
        opp.Volume_Last_Month__c=60;
        opp.Volume_Two_Months_Ago__c=50;
        opp.Volume_Three_Months_Ago__c =40;
        opp.Name='Test12';
        update opp;
        
        System.debug(opp);
        
        
           
        
        Test.stopTest();
    }

}

When i debug oldOpp and newOpp both return same value mnss it cannot get newer one can someone help me why is it so..   
Best Answer chosen by Sanu Verma
Maharajan CMaharajan C
Hi Sanu,

Pleas use the below Test Class to get the 100% code coverage;

@isTest
public class UpdaterelatedLead_test{
    @testSetup
    static void setup() {
        Account acc = new Account(Name = 'Test Acc');
        insert acc;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
            Volume_Last_Month__c=5,
            Volume_Two_Months_Ago__c=25,
            Volume_Three_Months_Ago__c =20,
            AccountId = acc.id,
            StageName = 'Prospecting',
            CloseDate = system.Today()
        );
        insert testOpportunity;
        
        Lead ld = new lead(
        LastName = 'Test Lead',
        Renewal_App__c = testOpportunity.id,
        Company = 'test Company',
        Status = 'Open - Not Contacted'
        );
        insert ld;
        
    }   
        @isTest
        static void testMethod1() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Last_Month__c=60;
        
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }
    
     @isTest
        static void testMethod2() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Two_Months_Ago__c=50;
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }
    
     @isTest
        static void testMethod3() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Three_Months_Ago__c =40;
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj

All Answers

bhanu_prakashbhanu_prakash
Hi Sana,
Mark as best answer, If it resloves !!
I have update your code now oppournity will update and you need to add functionlaity as per your logic
@isTest
public class SampleTest{
    @testSetup
    static void setup() {
        Account acc = new Account(Name = 'Test Acc');
        insert acc;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			opp.Volume_Last_Month__c=5;
			opp.Volume_Two_Months_Ago__c=25;
			opp.Volume_Three_Months_Ago__c =20;
            AccountId = acc.id
        );
        insert testOpportunity;
    } 	
	    @isTest
        static void testMethod1() {
        Opportunity opp = [SELECT Id, RecordTypeID, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Last_Month__c=60;
        opp.Volume_Two_Months_Ago__c=50;
        opp.Volume_Three_Months_Ago__c =40;
        update testOpportunity;
		
        test.starttest();

        test.stoptest();
    }

}
Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com (https://www.forcelearn.com)  ​ 
 
Maharajan CMaharajan C
Hi Sanu,

Pleas use the below Test Class to get the 100% code coverage;

@isTest
public class UpdaterelatedLead_test{
    @testSetup
    static void setup() {
        Account acc = new Account(Name = 'Test Acc');
        insert acc;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
            Volume_Last_Month__c=5,
            Volume_Two_Months_Ago__c=25,
            Volume_Three_Months_Ago__c =20,
            AccountId = acc.id,
            StageName = 'Prospecting',
            CloseDate = system.Today()
        );
        insert testOpportunity;
        
        Lead ld = new lead(
        LastName = 'Test Lead',
        Renewal_App__c = testOpportunity.id,
        Company = 'test Company',
        Status = 'Open - Not Contacted'
        );
        insert ld;
        
    }   
        @isTest
        static void testMethod1() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Last_Month__c=60;
        
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }
    
     @isTest
        static void testMethod2() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Two_Months_Ago__c=50;
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }
    
     @isTest
        static void testMethod3() {
        Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
        opp.Volume_Three_Months_Ago__c =40;
        System.test.startTest();
        update opp;
        System.test.stopTest();
    }

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
This was selected as the best answer
Sanu VermaSanu Verma
Thnkss Raj It help me to cover 100%..