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
Sudhir NarayanswamySudhir Narayanswamy 

Recursive is blocking code coverage

Hi, 
   
   We have AfterInsert and AfterUpdate method in a apex class this is not getting coverd in test class please suggest me how to cover this code logic in test class below is the test class

   
@isTest
//(SeeAllData=true)

private class test_WorkOrderInstallationTriggerClass{

    static testMethod void myUnitTest(){
        Test.startTest(); 
 
        Account acc = new Account(); 
        acc.CurrencyIsoCode = 'EUR';
        acc.Name= 'test';
        insert acc;
        Opportunity opp = new opportunity();
        opp.name = 'test';
        opp.AccountId = acc.id;
        opp.Type = 'New Business';
        opp.Sales_Area__c = 'NA Central TOLA';
        opp.CloseDate = System.today();
        opp.StageName = '2-Qualified';
        opp.CurrencyIsoCode = 'EUR';
        opp.Special_Instructions_for_Installation__c = 'test';
        opp.Installation_security_requirements__c = 'None';
        opp.type = 'New Business';
        insert opp;
        opp.Special_Instructions_for_Installation__c = 'test_update';
        update opp;
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User u = new User(Alias = 'SysAdm', Email='systemadmin@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='systemadmin12@testorg.com', Phone = '678836387');
                          insert u;       
        OpportunityTeamMember oppMember = new OpportunityTeamMember();
        oppMember.OpportunityId = opp.id;
        oppMember.TeamMemberRole = 'Account Executive (AE)';
        oppMember.userId = u.id;
        insert oppMember;
        OpportunityTeamMember oppMember1 = new OpportunityTeamMember();
        oppMember1.OpportunityId = opp.id;
        oppMember1.TeamMemberRole = 'Systems Engineer (SE)';
        oppMember1.userId = u.id;
        insert oppMember1;
        Sales_Order__c so = new Sales_Order__c();
        so.Name = 'test';
        so.Type__c = 'Beta';
        so.CurrencyIsoCode = 'EUR';
        so.Opportunity__c = opp.id;
        so.Shipment_Date__c = Date.newInstance(2015,7,27);
        so.Actual_Shipment_Date__c = Date.newInstance(2015,7,27);
        so.Tracking_Number__c = '5247';
        so.SO_Number__c='SO123';
        so.Shipping_Agent_Code__c = 'JIT';
        so.End_Customer_Contact_Name__c = 'test';
        so.End_Customer_Contact_Email__c = 'test@test.com';
        so.End_Customer_Contact_Phone__c = '897372';
        so.Ship_To_Company__c = 'test';
        so.Ship_To_Contact_Name__c = 'test';
        so.Ship_To_Contact_Phone__c = '87988098';
        so.Ship_To_Contact_Email__c = 'test@test.com';
        so.Ship_To_Address_1__c = '562 test';
        so.Ship_To_Address_2__c = '654 test';
        so.Ship_To_City__c = 'test';
        so.Ship_to_State__c = 'test';
        so.Ship_To_Zip_Postal_Code__c = '65787';
        so.Ship_To_Country__c = 'india';
        so.Ship_To_Contact_Name__c = 'test';
        insert so;
        so.Shipment_Date__c = Date.newInstance(2016,7,27);
        so.Actual_Shipment_Date__c = Date.newInstance(2016,7,27);
        so.Tracking_Number__c = '5244';
        so.Shipping_Agent_Code__c = 'AERO';
        so.Pro_Install_Flag__c = true;
        System.debug('test-----');         
        update so;        
        Sales_Order_Line__c soLine = new Sales_Order_Line__c();
        soLine.Sales_Order__c = so.id;
        soLine.CurrencyIsoCode = 'EUR';
        soLine.BOM_Line_Number__c='1';
        soLine.Package_Product_Code__c = 'test';
        soLine.Serial_Number__c = 'SO123';
        soLine.Quantity__c = 1;
        insert soLine;
        Installation_WorkOrder_Contacts__c insWOcontact = new Installation_WorkOrder_Contacts__c();
        insWOcontact.Name = 'test';
        insWOcontact.Email__c = 'test@test.com';
        insWOcontact.Phone__c = '89798890';
        insWOcontact.Role__c = 'coordinator';
        insert insWOcontact;
        Installation_WorkOrder_Contacts__c insWOcontact1 = new Installation_WorkOrder_Contacts__c();
        insWOcontact1.Name = 'test';
  //      insWOcontact1.Email__c = 'test@test.com';
        insWOcontact1.Phone__c = '89798890';
        insWOcontact1.Role__c = 'PSE';
        insWOcontact1.Security_Clear__c = 'TS-SCI clearance';
        insert insWOcontact1;
        WorkOrder__c wo = new WorkOrder__c();
        wo.Name='test';
        wo.Date_installer_confirms_receipt_of_work__c = datetime.newInstance(2015, 7, 28, 13, 5, 23);
        wo.installation_schedule_date_time__c = datetime.newInstance(2015, 7, 28, 13, 5, 23);
        wo.Verified_installation_location__c = true;
        wo.Verified_security_citizenship_rqmnt__c = true;
        wo.Sales_Order_Lookup__c=so.id;
        wo.Installer_coordinator_name1__c = insWOcontact.id;
        wo.PSE_Name1__c = insWOcontact1.id;
        wo.First_attempt_response__c = 'yes';
        wo.new_installation_schedule__c = datetime.newInstance(2015, 7, 28, 13, 5, 23);
        wo.Installation_validation_complete__c = Date.newInstance(2016,7,27);
        wo.Reason_for_escalation__c = 'CUSTOMER unresponsive to initial contact over a 4 day period';
        wo.Escalation_closed1__c = true;
        wo.Ship_to_Company__c='test';
        wo.Ship_to_City__c='test';
        wo.Ship_to_State__c='test';
        wo.Reason_for_escalation1__c = 'CUSTOMER unresponsive to initial contact over a 4 day period';
        wo.Escalation_closed2__c = true;
        wo.Support_Case_closed__c = true;
        wo.Nimble_Customer_Support_case__c = 'WO7892';
        wo.Pre_Inst_Approved__c = datetime.newInstance(2015, 7, 28, 13, 5, 23);
        wo.Escalation_Date_Time__c = datetime.newInstance(2015, 7, 28, 13, 5, 23);
        wo.Security_Requirement_Notes__c = 'TestClass';      
        insert wo;
        
        wo.installation_schedule_date_time__c = datetime.newInstance(2016, 7, 28, 13, 5, 23);
        //Added by Sudhir
        checkRecursive.resetRunOnceFlag(); 
        //checkRecursive.run = true;
        update wo;
         
         
        //System.assertEquals(so.Name,'test');
        //System.assertEquals(wo.Name,'test');
        
  
        Test.stopTest(); 
    } 
 }

 
jigarshahjigarshah
Sudhir, can you share the actual code that is not being covered within the test code coverage?
Sudhir NarayanswamySudhir Narayanswamy
Hi,

 Below is the actual code method AfterInsert and AfterUpdate is not covered please suggest me how to cover 
public class Installation_Work_Order_Trigger_Class extends triggerHandler 
{   
   
 
    public override void afterInsert(){
        System.debug('### Installation_Work_Order_Trigger_Class => afterInsert :: Entered');
        //list to hold the list of SO Recertified Work Order
        List<Sales_Order_Line__c> solListRecert = new List<Sales_Order_Line__c>();
        list<Sales_Order_Line__c> soListLine = new  list<Sales_Order_Line__c>();
        
        For(WorkOrder__c wo : (list<WorkOrder__c>)Trigger.new)
        {         
            
            if(wo.Sales_Order_Lookup__c != null){
                if(soLineMap.containskey(wo.Sales_Order_Lookup__c))
                soListLine = soLineMap.get(wo.Sales_Order_Lookup__c);
                System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => soListLine values :: ' + soListLine);
                
                //added by Unnat
                if(somap_ids.get(wo.Sales_order_lookup__c).Re_Certification_Flag__c == true) {
                    System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => Recertification Flag :: True');
                    solListRecert = soLineMap.get(wo.Sales_Order_Lookup__c);
                    System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => solListRecert values :: ' + solListRecert);
                }
                
            }
        }
        //Added by Unnat :: separated the for loops that was connected with above to retrieve the case info
        assetToCase = retreiveCase(solListRecert); //retrieve the case value
        System.debug('### Installation_Work_Order_Trigger_Class => after Insert => assetToCase Map values :: ' + assetToCase);
        For(WorkOrder__c wo : (list<WorkOrder__c>)Trigger.new)
        { 
            if(soListLine.size()>0){
                For(Sales_Order_Line__c newList : soListLine )
                {
               
  if(newList.BOM_Line_Number__c.isNumeric())
                    {
                        WO_Sales_Order_Lines__c woLine = new WO_Sales_Order_Lines__c();
                        if(newList.name!=null && newList.name!='')
                        {
                            woLine.Name=newList.name;
                        }
                        if(newList.BOM_Line_Number__c!=null && newList.BOM_Line_Number__c!='')
                        {
                            woLine.BOM_Line_Number__c = newList.BOM_Line_Number__c;
                        }
                        if(newList.Package_Product_Code__c!=null && newList.Package_Product_Code__c!='')
                        {
                            woLine.Package_Product_Code__c=newList.Package_Product_Code__c;
                        }                            
                        woLine.Product__c=newList.Product__c;               
                        if(newList.Serial_Number__c!=null && newList.Serial_Number__c!='')
                        {
                            woLine.Serial_Number__c=newList.Serial_Number__c;
                        }
                        if(newList.Quantity__c!=null)
                        {
                            woLine.Quantity__c=newList.Quantity__c;
                        }
                        if(newList.CurrencyIsoCode!=null && newList.CurrencyIsoCode!='')
                        {
                            woLine.CurrencyIsoCode=newList.CurrencyIsoCode;
                        }
                        if(newList.Nimble_Version__c !=null && newList.Nimble_Version__c !='')
                        {
                            woLine.Nimble_Version__c=newList.Nimble_Version__c;
                        }
                        if(newList.Sale_Price__c !=null )
                        {
                            woLine.Sale_Price__c =newList.Sale_Price__c;
                        }
                        //added by Unnat
                        //if condition to associate the case to the work order line item
                        if (somap_ids.get(wo.Sales_order_lookup__c).Re_Certification_Flag__c == true) {
                            System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => Serial Number is :: '+ newList.Serial_Number__c);
                            if (newList.Serial_Number__c != null) {
                                System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => assetToCase size :: '+ assetToCase.size());
                                if (assetToCase.size() > 0 ) {
                                    woLine.case__c = assetToCase.get(newList.Serial_Number__c).id;
                                    woLine.Nimble_Version__c = assetMap.get(newList.Serial_Number__c).assetNimbleOsVersion__c;
                                    woLine.Package_Product_Code__c = assetMap.get(newList.Serial_Number__c).Package_Product_Code_1__c;
                                }
                            }
                        }                           
                        woLine.Installation_WorkOrder__c=wo.id;
                        woLine.Sales_Order_Line_ID__c  = newList.Id;                    
                        woListLine.add(woLine);
                    }                           
                }
            }
        }

            //Utility.runDupRecTrigger=false;
            if(!Test.isRunningTest())
                Utility.runWorkOrderTrigger= false;
            
        if(!Test.isRunningTest()&& woListLine.size()>0) {
            System.debug('### Installation_Work_Order_Trigger_Class => afterInsert => WO Line List :: '+ woListLine);
            insert woListLine; 
        }
        
    }
    
     
}

 
Sudhir NarayanswamySudhir Narayanswamy
There is another method in the same code due to limitation i was not able to add in above thread 
 
public override void afterUpdate(){
        System.debug('afterUpdate-------');
        list<Sales_Order__c> soList = new list<Sales_Order__c>();
        List<Sales_Order_Line__c> solListRecert = new List<Sales_Order_Line__c>();

        For(WorkOrder__c w : [select Sales_Order_Lookup__r.Shipment_Date__c,Sales_Order_Lookup__r.Actual_Shipment_Date__c,Sales_Order_Lookup__r.Shipping_Agent_Code__c,Sales_Order_Lookup__r.Tracking_Number__c,scheduled_ship_date__c,actual_ship_date__c,Carrier__c,Tracking_1__c from WorkOrder__c where id in : Trigger.new])
        {
            workorderMapId.put(w.Sales_Order_Lookup__c,w);
            woInsertId.add(w.Sales_Order_Lookup__c);
            //System.debug('workorderMapId-------' + workorderMapId);  

            //added by Unnat
            if(somap_ids.get(w.Sales_order_lookup__c).Re_Certification_Flag__c == true) {
                System.debug('### Installation_Work_Order_Trigger_Class => afterUpdate => Recertification Flag :: True');
                solListRecert = soLineMap.get(w.Sales_Order_Lookup__c);
                System.debug('### Installation_Work_Order_Trigger_Class => afterUpdate => solListRecert values :: ' + solListRecert);
            }         
        }
        For(Sales_Order__c s : [select Shipment_Date__c,Actual_Shipment_Date__c,Shipping_Agent_Code__c, Tracking_Number__c from Sales_Order__c where id in : woInsertId])
        {
            sorderID.put(s.id,s);
        }
        //System.debug('soIds@@@@-----' + soIds);
        List<WO_Sales_Order_Lines__c> woSOLines = [select id from WO_Sales_Order_Lines__c where Installation_WorkOrder__c in : Trigger.new];
        //System.debug('woSOLines------' + woSOLines);
        if(woSOLines.size()>0){
            delete woSOLines;
        }

        assetToCase = retreiveCase(solListRecert); //retrieve the case value

        For(WorkOrder__c wo : (list<WorkOrder__c>) Trigger.new)
        {
            System.debug('wo------' + wo);          
            list<Sales_Order_Line__c> soListLine = new List<Sales_Order_Line__c>();
            if(wo.Sales_Order_Lookup__c != null)
                soListLine = soLineMap.get(wo.Sales_Order_Lookup__c);
            //System.debug('soListLine------' + soListLine);
            
            if(soListLine.size()>0){
            For(Sales_Order_Line__c newList : soListLine )
            {

                System.debug('newList-------' + newList);                        
                if(newList.BOM_Line_Number__c.isNumeric())
                {
                    System.debug('in----');
                    WO_Sales_Order_Lines__c woLine = new WO_Sales_Order_Lines__c();
                    
                    if(newList.name!=null && newList.name!='')
                    {
                        woLine.Name=newList.name;
                    }
                    if(newList.BOM_Line_Number__c!=null && newList.BOM_Line_Number__c!='')
                    {
                        woLine.BOM_Line_Number__c = newList.BOM_Line_Number__c;
                    }
                    if(newList.Package_Product_Code__c!=null && newList.Package_Product_Code__c!='')
                    {
                        woLine.Package_Product_Code__c=newList.Package_Product_Code__c;
                    }                            
                    woLine.Product__c=newList.Product__c;               
                    if(newList.Serial_Number__c!=null && newList.Serial_Number__c!='')
                    {
                        woLine.Serial_Number__c=newList.Serial_Number__c;
                    }
                    if(newList.Quantity__c!=null)
                    {
                        woLine.Quantity__c=newList.Quantity__c;
                    }
                    if(newList.CurrencyIsoCode!=null && newList.CurrencyIsoCode!='')
                    {
                        woLine.CurrencyIsoCode=newList.CurrencyIsoCode;
                    } 
                    if(newList.Nimble_Version__c !=null && newList.Nimble_Version__c !='')
                    {
                        woLine.Nimble_Version__c=newList.Nimble_Version__c;
                    }
                    if(newList.Sale_Price__c !=null )
                    {
                        woLine.Sale_Price__c =newList.Sale_Price__c;
                    }
                    
                    if (somap_ids.get(wo.Sales_order_lookup__c).Re_Certification_Flag__c == true) {
                        System.debug('### Installation_Work_Order_Trigger_Class => afterUpdate => Serial Number is :: '+ newList.Serial_Number__c);
                        if (newList.Serial_Number__c != null) {
                            System.debug('### Installation_Work_Order_Trigger_Class => afterUpdate => assetToCase size :: '+ assetToCase.size());
                            if (assetToCase.size() > 0 ) {
                                woLine.case__c = assetToCase.get(newList.Serial_Number__c).id;
                                woLine.Nimble_Version__c = assetMap.get(newList.Serial_Number__c).assetNimbleOsVersion__c;
                                woLine.Package_Product_Code__c = assetMap.get(newList.Serial_Number__c).Package_Product_Code_1__c;
                            }
                        }
                    }              
                    woLine.Installation_WorkOrder__c=wo.id;
                    woLine.Sales_Order_Line_ID__c  = newList.Id;
                    woListLine.add(woLine);
                    //System.debug('woListLine----' + woListLine);
                }                           
            }
            }
            
            if(!Test.isRunningTest() && woListLine.size()>0)
            insert woListLine;          
        }
        if(Test.isRunningTest())
            Utility.runWorkOrderTrigger = false;  
        
    }