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
shravani milshravani mil 

unable to cover onundelete method in test class

unable to cover below lines in my test class:

public void onUndelete(List<Opportunity> oldOpps) {
        
        List<AccountShare> acctSharesToAdd = new List<AccountShare>();  
        
        for (Opportunity o : oldOpps) {
            
            ProvideAccShare(o,acctSharesToAdd);
        }
        
        if (acctSharesToAdd.size() > 0)
            insert acctSharesToAdd;
    }
    
LBKLBK
Hi Shravani,
It should be straight forward. Something like this.
List<Opportunity> lstOpp = new List<Opportunity>(); 

Account acc1 = new Account(); 
acc1.name = 'acc1'; 
insert acc1; 

Opportunity opp1 = new Opportunity(); 
opp1.Name = 'Opp1'; 
opp1.amount = 10000; 
opp1.account = acc1; 
opp1.closedate = System.today(); 
insert opp1; 
lstOpp.add(opp1); 

Opportunity opp2 = new Opportunity(); 
opp2.Name = 'Opp1'; 
opp2.amount = 10000; 
opp2.account = acc1; 
opp2.closedate = System.today(); 
insert opp2; 
lstOpp.add(opp2); 

test.startTest(); 
	onUndelete(lstOpp); 
test.stopTest();
Please add the above code in a proper testclass / method and add assertion.

Please post your test class, if you need further help.

 
Saravana Bharathi 1Saravana Bharathi 1
If it is invoking from trigger on undelete event, then you can do this.

List<Opportunity> lstOpp = new List<Opportunity>();
Account acc1 = new Account();
acc1.name = 'acc1';
insert acc1;
 
Opportunity opp1 = new Opportunity();
opp1.Name = 'Opp1';
opp1.amount = 10000;
opp1.account = acc1;
opp1.closedate = System.today();

lstOpp.add(opp1);
insert lstOpp;

delete lstOpp;

lstOpp = [Select Id From Opportunity ALL ROWS];

undelete lstOpp;

Thanks
Arvind KumarArvind Kumar
Hi Shravani,

Follow the below code, it will help you.
 
Account accObj = new Account();
accObj.name = 'Test Account';
insert accObj;

List<Opportunity> oppList = new List<Opportunity>();

Opportunity oppObj = new Opportunity();

oppObj.Name = 'Opp1';
oppObj.amount = 10000;
oppObj.accountId= accObj.Id;
oppObj.closedate = System.today();

oppList.add(oppObj);

Test.startTest();
insert oppList;

delete oppList;

oppList = [Select Id From Opportunity ALL ROWS];

undelete oppList;
Test.stopTest();


Thanks,
Arvind Kumar

shravani milshravani mil
Hi Thank for the code But am not covering anything with the above code. Thanks