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
VishwanathVishwanath 

How to cover for loop

Hi, please any one help me out from  this issue,

im writing the test class for the trigger and im not able to cover the code that comes under for loop,

 

Trigger code

trigger PayHist_insert_Under_Agr_ProcessUnit on Royalty_Payment_History__c (before insert)
 {
 if
 (!TriggerRecurHelp.hasAlreadyCreatedRecursiv())
 {
 set<String> agid=new set<String>();
 set<id> pid=new set<id>();
 id ppid;
 for(Royalty_Payment_History__c UOP:Trigger.new)
 {
 ppid=UOP.Process_Unit__c;
 agid.add(UOP.Aggregation_Group__c);
 pid.add(UOP.Process_Unit__c);
 }
 Process_unit__c pp=[select id,name,Aggregation_Group__r.id from Process_Unit__c where id=:ppid];
 id aagid=pp.Aggregation_Group__r.id;
 UOP_Aggregation_Group__c aa;id prid;
 if(pp.Aggregation_Group__r.id!=null)
 {
 aa=[select id,name from UOP_Aggregation_Group__c where id=:aagid];
 prid=aa.id;
 }
 List<Process_Unit__c> pc=new List<PRocess_Unit__c>();
 pc=[select id,name from Process_Unit__c where Aggregation_Group__r.id=:prid and id!=:ppid];
 System.debug('total PU'+pc.size());
 List<Royalty_Payment_History__c> 
 insrtpay=new List<Royalty_Payment_History__c>();   
 Royalty_Payment_History__c inpay=new Royalty_Payment_History__c();   
 for(Royalty_Payment_History__c U:Trigger.new)  
 {    
 for(Process_Unit__c p:pc) 
 {    
 insrtpay.add(new Royalty_Payment_History__c(
 Payment_History_Event__c=U.Payment_History_Event__c,
 Process_Unit__c=p.id,    
 UOP_Year__c=U.UOP_Year__c,      
 Royalty_Type__c=U.Royalty_Type__c,   
 Comments__c=U.Comments__c,      
 Applicable_BLS__c=U.Applicable_BLS__c,   
 Application__c=U.Application__c,    
 Application_Id__c=U.Application_Id__c,       
 Capacity_Reporting_Inaccuracy__c=U.Capacity_Reporting_Inaccuracy__c,  
 BLS_Base__c=U.BLS_Base__c,       
 Created_By_User_Id__c=U.Created_By_User_Id__c,   
 Due_Date__c=U.Due_Date__c,      
 Invoice_Ammount_For_FA_DA__c=U.Invoice_Ammount_For_FA_DA__c,   
 Invoice_Amount__c=U.Invoice_Amount__c,      
 Invoice_Date__c=U.Invoice_Date__c,       
 Invoice_Number__c=U.Invoice_Number__c,   
 Invoice_Paid_Date__c=U.Invoice_Paid_Date__c,   
 Last_Modified_By_User_Id__c=U.Last_Modified_By_User_Id__c,     
 Mail__c=U.Mail__c,      
 New_Fully_Paid_Capacity__c=U.New_Fully_Paid_Capacity__c,   
 Overage__c=U.Overage__c,      
 Previous_Fully_Paid_Capacity__c=U.Previous_Fully_Paid_Capacity__c,   
 Process_Type__c=U.Process_Type__c,      
 Purchase_Order_No__c=U.Purchase_Order_No__c,  
 Report_Processed_Date__c=U.Report_Processed_Date__c,    
 Royalty_Rate__c=U.Royalty_Rate__c,  
 Sales_Order_No__c=U.Sales_Order_No__c,  
 Throughput__c=U.Throughput__c,      
 Units_of_Measure__c=U.Units_of_Measure__c));  
 }  
 }   
 TriggerRecurHelp.sethasAlreadyCreatedRecursiv(); 
 System.debug('insrtpay'+insrtpay.size()); 
 if (!insrtpay.isEmpty())
 {    
 try{  
 insert insrtpay;  
 }catch(Exception e){} 
 }       
 }     
 }
 

  test class code:

@istest()
Public class PayHist_insert_Under_Agr_ProcessUnittest
{
public static testmethod void test()
{
Aggregation_Group__c UOP=new Aggregation_Group__c();
UOP.name='testAgg';
insert UOP;

Process_Unit__c pp=new Process_Unit__c();
pp.name='test';
pp.Aggregation_Group__c=UOP.id;
insert pp;


Royalty_Payment_History__c UPay=new Royalty_Payment_History__c();
UPay.Payment_History_Event__c='Report';   
 UPay.Process_Unit__c=pp.id;     
 UPay.UOP_Year__c='2000';      
 insert Upay;

 
 List<Royalty_Payment_History__c> UPay1=new List<Royalty_Payment_History__c>();
 
 Royalty_Payment_History__c uu=new Royalty_Payment_History__c(Payment_History_Event__c=Upay.Payment_History_Event__c,Process_Unit__c=pp.id,UOP_Year__c=Upay.UOP_Year__c);
 UPay1.add(uu);
 
 
 insert Upay1;
 
}

}

 

Thank you

:

 

Navatar_DbSupNavatar_DbSup

Hi,

You can try this one

 

@istest()
Public class PayHist_insert_Under_Agr_ProcessUnittest
{

public static testmethod void test()
{
set<String> agid=new set<String>();
set<id> pid=new set<id>();
id ppid;
Aggregation_Group__c UOP=new Aggregation_Group__c();
UOP.name='testAgg';
insert UOP;

Process_Unit__c pp=new Process_Unit__c();
pp.name='test';
pp.Aggregation_Group__c=UOP.id;
insert pp;

Royalty_Payment_History__c UPay=new Royalty_Payment_History__c();
UPay.Payment_History_Event__c='Report'; 
UPay.Process_Unit__c=pp.id; 
UPay.UOP_Year__c='2000'; 
insert Upay;
Process_Unit__c pp1=new Process_Unit__c();
pp1.name='test';
pp1.Aggregation_Group__c=UOP.id;
insert pp1;

List<Royalty_Payment_History__c> UPay1=new List<Royalty_Payment_History__c>();
Royalty_Payment_History__c uu=new Royalty_Payment_History__c(Payment_History_Event__c=Upay.Payment_History_Event__c,Process_Unit__c=pp.id,UOP_Year__c=Upay.UOP_Year__c);
UPay1.add(uu);
insert Upay1;
}

}

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

Chamil MadusankaChamil Madusanka

Hi,

Which for loop you are taking about? I mean there are for loops. Which one you couldn't cover?

VishwanathVishwanath

Hi,

 Thanks for your replay, i tried your code, but nothing is happening the same coverage is showing (41%)

 

VishwanathVishwanath

Hi Chamil M,

 

im having prbm  inside the below for loops

for(Royalty_Payment_History__c U:Trigger.new) 
{   
for(Process_Unit__c p:pc)
{

 

}

}

Navatar_DbSupNavatar_DbSup

Hi,

 

Can you tell me what value are  you getting from following statement when executing the test method which I have posted? So that I can help to resolve your problem

 

 System.debug('total PU'+pc.size());

 

 

 

VishwanathVishwanath

Hi,

 

it showing Zero