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
Rohini Chaudhary 14Rohini Chaudhary 14 

Hii All ,can anyone help me in increasing the code coverage of this trigger handler test class to 100%? Rn Code Coverage is 87%

Apex class - 

public with sharing class InvoiceLineTriggerHandler {

    public class InvoiceLineSchedule {
        public Date lineSvcStartDate {get; set;}
        public Date invcLineSvcDeliveryDate {get; set;}
        public String invcScheduleType {get; set;}
    }
    
    public static void runtrigger(List<kugo2p__KugamonInvoiceLine__c> newList, List<kugo2p__KugamonInvoiceLine__c> oldList, Map<id,kugo2p__KugamonInvoiceLine__c> newMap, Map<id,kugo2p__KugamonInvoiceLine__c> oldMap)
    {    
        if(trigger.isAfter && trigger.isInsert) {
            afterInsert(newList,newMap);
        }
    }
    
    private static void afterInsert(List<kugo2p__KugamonInvoiceLine__c> newList, Map<Id,kugo2p__KugamonInvoiceLine__c> newMap) {
        // Update the Service terms on the Invoice level
        updateInvoiceServiceDates (newList, newMap);
    }

    private static void updateInvoiceServiceDates (List<kugo2p__KugamonInvoiceLine__c> newList, Map<Id,kugo2p__KugamonInvoiceLine__c> newMap) {
        Set<Id> invcIds = new Set <Id> ();
        Map<Id,InvoiceLineSchedule> invcScheduleMap = new Map<Id,InvoiceLineSchedule> ();

        for (kugo2p__KugamonInvoiceLine__c eachInvoiceLine: newList) {
            invcIds.add (eachInvoiceLine.kugo2p__Invoice__c);
        }

        List <kugo2p__KugamonInvoiceLine__c> ivcLines = [Select id, kugo2p__Invoice__c , kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceDelivered__c, kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceStart__c, kugo2p__SalesOrder__r.kugo2p__BillingFrequency__c, kugo2p__SalesOrder__r.kugo2p__InvoiceSchedule__c from kugo2p__KugamonInvoiceLine__c Where kugo2p__Invoice__c in :invcIds WITH SECURITY_ENFORCED];

        for(kugo2p__KugamonInvoiceLine__c eachInvcLine:ivcLines){

            InvoiceLineSchedule invcSchedule; 
                system.debug('Nandita>>>>>>>>>>>>>>>>>>>>>>'+invcSchedule);
            if (invcScheduleMap.containsKey(eachInvcLine.kugo2p__Invoice__c)) {
                 
            
                invcSchedule = invcScheduleMap.get(eachInvcLine.kugo2p__Invoice__c);

                if (eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceStart__c < invcSchedule.lineSvcStartDate) {
                    invcSchedule.lineSvcStartDate = eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceStart__c;
                }

                if (eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceDelivered__c > invcSchedule.invcLineSvcDeliveryDate) {
                    invcSchedule.invcLineSvcDeliveryDate = eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceDelivered__c;
                }
            } else {
                invcSchedule = new InvoiceLineSchedule ();
                invcSchedule.lineSvcStartDate = eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceStart__c;
                invcSchedule.invcLineSvcDeliveryDate = eachInvcLine.kugo2p__ServiceDeliverySchedule__r.kugo2p__DateServiceDelivered__c;
                invcSchedule.invcScheduleType = eachInvcLine.kugo2p__SalesOrder__r.kugo2p__InvoiceSchedule__c;
                invcScheduleMap.put (eachInvcLine.kugo2p__Invoice__c, invcSchedule);
            }
        }

        List <kugo2p__KugamonInvoice__c> invcList = [Select id, Service_Start_Date__c, Service_End_Date__c from kugo2p__KugamonInvoice__c where id in :invcIds WITH SECURITY_ENFORCED];

        List <kugo2p__KugamonInvoice__c> invcListUpdate = new List <kugo2p__KugamonInvoice__c> ();

        for (kugo2p__KugamonInvoice__c eachInvoice : invcList) {
            if (invcScheduleMap.containsKey (eachInvoice.id)) {
                eachInvoice.Service_Start_Date__c = invcScheduleMap.get (eachInvoice.id).lineSvcStartDate;
                eachInvoice.Service_End_Date__c = invcScheduleMap.get (eachInvoice.id).invcLineSvcDeliveryDate;
                invcListUpdate.add (eachInvoice);
            }
        }

        if ( !invcListUpdate.isEmpty () ) {
            // Database.update(invcListUpdate); // Moved to Future Method
            updateInvoices(JSON.serialize(invcListUpdate));
        }  
    }

    @future
    private static void updateInvoices(String invoiceList){
        List<kugo2p__KugamonInvoice__c> lstInvoices = (List<kugo2p__KugamonInvoice__c>)JSON.deserialize(invoiceList, List<kugo2p__KugamonInvoice__c>.class);
        Database.update(lstInvoices);
    } 
}

Test Class:

@isTest
private class InvoiceLineTriggerHandlerTest {
    @isTest
    static void customDataCreateForInvoiceLineTrigger(){
        List <Trigger_Execution__c> triggerExecList = new List <Trigger_Execution__c> ();
         Trigger_Execution__c invoiceLineTriggerCustomSetting = new Trigger_Execution__c (Name = 'InvoiceLineTrigger', is_Execute__c = true);
         Trigger_Execution__c invoiceTriggerCustomSetting = new Trigger_Execution__c (Name = 'InvoiceTrigger', is_Execute__c = true);
         Trigger_Execution__c orderTriggerCustomSetting = new Trigger_Execution__c (Name = 'OrderTrigger', is_Execute__c = true);
        triggerExecList.Add (invoiceLineTriggerCustomSetting);
        triggerExecList.Add (invoiceTriggerCustomSetting);
        triggerExecList.Add (orderTriggerCustomSetting);         
        insert triggerExecList; 
    }

    @isTest
    static void test1() {
       // try{
            customDataCreateForInvoiceLineTrigger();
            Test.startTest();
            //account
            Account testAccount = TestKugamonDataFactory.createAccount();
            Insert testAccount;
            
            //contact
            Contact testContact = TestKugamonDataFactory.createContact(testAccount);
            Insert testContact;
            
            //custom pricebook
            Pricebook2 testCustomPriceBook = TestKugamonDataFactory.createCustomPriceBook();
            Insert testCustomPriceBook;
            
            //kugamon settings
            kugo2p__KugamonSetting__c kugamonSettings = TestKugamonDataFactory.createKugamonSettings(testCustomPriceBook, new kugo2p__Warehouse__c());
            Insert kugamonSettings;
            
            //warehouse
            kugo2p__Warehouse__c testWarehouse = TestKugamonDataFactory.createWarehouse(kugamonSettings);
            Insert testWarehouse;
            
            //additional account info
            kugo2p__AdditionalAccountDetail__c additionalAccountInfo = TestKugamonDataFactory.createAdditionalAccountInfo(testAccount, testWarehouse);
            //insert additionalAccountInfo;
            
            //product
            List<Product2> lstProducts = TestKugamonDataFactory.createProducts();
            Insert lstProducts;

            //Additional Product detail
            kugo2p__AdditionalProductDetail__c apds = new kugo2p__AdditionalProductDetail__c(Name='PROMO 1 Year End User 5,000-9,999 Employees', kugo2p__ProductDescription__c = 'SecureSuite Membership End User 5,000-9,999 Employees',
            kugo2p__ProductFamily__c = 'SecureSuite', kugo2p__ReferenceProduct__c = lstProducts[0].Id, kugo2p__Service__c = true, 
            kugo2p__StandardPrice__c = 10978, kugo2p__UnitCost__c = null, kugo2p__Active__c = true, kugo2p__PrintProductTextandTerms__c = true, 
            kugo2p__KitBundle__c = true, kugo2p__KitBundlePricing__c = 'Member Prices Only', kugo2p__UnitofTerm__c = 'Year', kugo2p__ProductCode__c = 'CIS-SSM-SLTT');
            
            
            //Create Pricebook Entries
            list<PricebookEntry> lstPricebookEntry = TestKugamonDataFactory.createPricebookEntries(lstProducts, Test.getStandardPricebookId(), testCustomPriceBook);
            Insert lstPricebookEntry;
            
            List<kugo2p__TaxLocation__c> lstTaxLocations = TestKugamonDataFactory.createTaxLocation(testAccount, additionalAccountInfo, kugamonSettings);
            Insert lstTaxLocations;
            
            id RecordType1=[SELECT Id,DeveloperName  FROM RecordType WHERE sObjectType = 'Opportunity' AND Name = 'New'].id;
            
            //opportunity
            Opportunity testOpportunity = TestKugamonDataFactory.createOpportunity(testAccount, testCustomPriceBook, 'Closed/Won');
            testOpportunity.kugo2p__SyncSource__c = 'Primary Order';
            testOpportunity.RecordTypeId=RecordType1;
            Insert testOpportunity;
            
            //Create Opportunity Line Items
            list<OpportunityLineItem> lstOpportunityLineItems = TestKugamonDataFactory.createOpportunityLineItems(testOpportunity, lstPricebookEntry);
            Insert lstOpportunityLineItems;
            
            //order
            id RecordType=[SELECT Id,DeveloperName  FROM RecordType WHERE sObjectType = 'kugo2p__SalesOrder__c' AND Name = 'New'].id;
            kugo2p__SalesOrder__c testSalesOrder = TestKugamonDataFactory.createSalesOrder(testAccount, testContact, testOpportunity, testCustomPriceBook, testWarehouse);
            testSalesOrder.RecordTypeId=RecordType;
            Insert testSalesOrder;
            
            //invoice
            List<kugo2p__KugamonInvoice__c> testInvoice = TestKugamonDataFactory.createInvoices(testAccount, testSalesOrder);
            testInvoice[0].kugo2p__InvoiceDueDate__c = system.today().addMonths(6);
            Insert testInvoice;
            kugo2p__KugamonInvoice__c testInvoice2=[Select id, Opportunity_Record_Type__c, Date_Difference__c     from kugo2p__KugamonInvoice__c where Id=: testInvoice[0].id];
            system.debug('Date difference1-'+testInvoice2.Date_Difference__c);
            
            //invoice lines
            List<kugo2p__KugamonInvoiceLine__c> invcLines = TestKugamonDataFactory.createInvliceLines(testSalesOrder,apds,testInvoice[0]);
            Insert invcLines;
            //system.debug('invoiceLines '+invcLines);
            Test.stopTest();
       /* }catch(DmlException e){
            System.debug('A DML exception has occurred: ' +
                         e.getMessage());
        }  */       
        
        
    }
}
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Rohini,

As these are your custom objects and it would be hard to try in personal org and suggest you.It would be easy if you share the screenshot of the part which the test class is not covering so experts can suggest based on it.

Thanks,
 
Rohini Chaudhary 14Rohini Chaudhary 14
Hi Sai,
Below is the part of code which is not covered.
User-added image