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
Tashika GuptaTashika Gupta 

my test class is not covering foor loop in trigger,how can i increase my code coverage

the portion with bold are not covered
here is my trigger:

/**
    Description : Calculate the weekending date and reporting month to be shown in GRN report
    Test Class  :
**/
/*
    Created By : Binit Ranjan
    Created On : 19/3/2013
*/

trigger tRIIO_POS_Invoice_UpdateReportMonth on tRIIO_PO_Invoice_Queries__c (Before Update,Before Insert) {

    Map<Id,Date> MapGetWeek=new Map<Id,date>();
    Map<Id,Date> MapGetNew =new Map<Id,date>();
    Map<Id,Date> MapGetOld=new Map<Id,date>();
    Map<Id,String> MapGetMonth=new Map<Id,String>();
    Map<Id,Date> MapGetFC=new Map<Id,Date>();
    Map<Id,String> MapGetFCC=new Map<Id,String>();
    list<id> lstAllNGAWeekIds =new list<id>();
    list<id> lstAllFCIds =new list<id>();
    list<Date> lstAllWeekIds =new list<Date>();
    
    For(tRIIO_PO_Invoice_Queries__c PRNew : trigger.New){
        MapGetNew.put(PRNew.Id, PRNew.Weekending__c);
        lstAllWeekIds.add(PRNew.Weekending__c);
    }    
       for(Week__c WeekData : [Select id, Name,h_Weekending__c,Reporting_Month__c from Week__c where h_Weekending__c in: lstAllWeekIds]){
           MapGetWeek.Put(WeekData.Id,WeekData.h_Weekending__c);
           MapGetMonth.Put(WeekData.Id,WeekData.Reporting_Month__c);
           lstAllNGAWeekIds.add(WeekData.id);

       }
        for(tRIIO_POS_Financial_Calendar__c FC : [Select id,Week_Commencing__c,Period_Number__c from tRIIO_POS_Financial_Calendar__c ]){
            MapGetFC.Put(FC.Id,FC.Week_Commencing__c);
            MapGetFCC.Put(FC.Id,FC.Period_Number__c);
            lstAllFCIds.add(FC.id);

        }

        for(tRIIO_PO_Invoice_Queries__c PR : Trigger.New)
        {
              For(Id WeekId :lstAllNGAWeekIds ){           
                if((MapGetWeek.get(WeekId))!= Null){
                    if(Date.valueof(PR.Weekending__c)== MapGetWeek.get(WeekId)){
                        PR.Reporting_Month__c = MapGetMonth.get(WeekId);
                        break;

                    }
                }
               }
               For(Id FIC:lstAllFCIds){           
                   if(MapGetFC.get(FIC)!=Null){
                     if(PR.Weekending__c == MapGetFC.get(FIC)){   
                       PR.h_FinancialPeriodNo__c = MapGetFCC.get(FIC);
                       break;

                     }
                   }  
               }    
             
               PR.h_WeekEnding__c = PR.Weekending__c;
               
        }
 }


here is my test class:

@istest
public class tRIIO_POS_Invoice_UpdateReportMonth_Test {
    public static testmethod void myUnitTest(){
        
          try{
                     
        
        
        tRIIO_Contract__c contract = new tRIIO_Contract__c (name = '1111', Description__c = 'Test Contract XXX');
        insert contract;
              
        tRIIO_POS_Supplier__c TestSupplier= new tRIIO_POS_Supplier__c(status__c = 'Approved',Approval_Date__c = system.today());
        insert TestSupplier;
             
        tRIIO_Purchasing_Depot__c Testdepot = new tRIIO_Purchasing_Depot__c (name = 'Depot1', Locality__c = '33', Location__c = 'XXXX', Town__c = 'Ars',
                                                                           County__c = 'test',  Phone_Number__c = '12345' );
        insert Testdepot;   
           
        triio_Personnel__c personnel = new triio_Personnel__c(name = 'xxxxyy');                                                    
        insert personnel;
              
        tRIIO_POS_Supplier_Rate__c ObjPOSSupplierRate = new tRIIO_POS_Supplier_Rate__c( Item_Description__c='abcd',Item_Short_Description__c='xyz',Unit__c='LOAD',Supplier__c=TestSupplier.ID);
        insert  ObjPOSSupplierRate;
              
      
        triio_pos_Agent__c TestAgent= new triio_pos_Agent__c();
        TestAgent.Employee_Display_Name__c = personnel.Id;
        insert TestAgent;  
      
        
        tRIIO_POS_Purchase_Order_Header__c purchasing = new tRIIO_POS_Purchase_Order_Header__c ( Contract__c = contract.id,
                                Depot__c = Testdepot.id,                                
                                Supplier__c = TestSupplier.id,
                                Order_to_Collect__c = true,
                                Requested_By_Project_Manager__c = TestAgent.id,
                                Deliver_to__c = 'My Delivery',
                                Order_Date__c = system.today(),
                                Required_Delivery_Date__c = system.today() ,Status__c='PO Items Raised');
        insert purchasing ;
      
        
        tRIIO_POS_Purchase_Order_Header__c purchaseHeader = [SELECT ID, Status__c FROM tRIIO_POS_Purchase_Order_Header__c WHERE id =:purchasing.id];
        system.assert(purchaseHeader.Status__c=='PO Items Raised');
        
         

        tRIIO_POS_Purchase_Order_Item__c ObjPOSPurchaseOrderItm = new tRIIO_POS_Purchase_Order_Item__c (Supplier_Rates__c=ObjPOSSupplierRate.id,Order_Qty__c=10.00,Ordered_Price__c=20.00,Purchasing__c=purchasing.ID);
        insert ObjPOSPurchaseOrderItm;
     
       
        tRIIO_PO_Invoice_Queries__c purchaseInvoice = new tRIIO_PO_Invoice_Queries__c(Purchasing_Items__c =ObjPOSPurchaseOrderItm.id,
        Q_Number__c='Q123456',Quantity__c=10.00,Rate_Price__c=10,Query_Dates__c=system.today());
        insert purchaseInvoice ;
         
        tRIIO_PO_Invoice_Queries__c purchaseInvoice1 =[select Weekending__c from tRIIO_PO_Invoice_Queries__c where  id =:purchaseInvoice.Id];
       
         
                    
        Week__c WK = new Week__c(name='03 May 13',Reporting_Month__c='May 13',h_Weekending__c=purchaseInvoice1.Weekending__c);
        insert WK;
        system.assertEquals(purchaseInvoice1.Weekending__c, WK.h_Weekending__c);
     
                     
       Week__c WK1 =[select id,name,Reporting_Month__c,h_Weekending__c from Week__c where id=:WK.Id];
              system.debug(WK1);
              
       tRIIO_POS_Financial_Calendar__c ObjPOSFinanceCal = new tRIIO_POS_Financial_Calendar__c(Week_Commencing__c=system.today(),Period_Number__c='1234');
              insert ObjPOSFinanceCal;
          }
       
 catch(exception e){
        }
    }

}
praveen murugesanpraveen murugesan
Hi Tashika,

Try to update week__c obj record. remaining will get covered.

Thanks.
Tashika GuptaTashika Gupta
HI Praveen,

Its not working.

Thanks