How to get the code coverage catch block in test class?

829    Asked by DavidEDWARDS in Salesforce , Asked on Feb 23, 2023

I'm unable to cover the code coverage for catch block.Kindly help me to cover that code lines as well.

Apex Class:

public without sharing class ZTS_EU_Contact_populateCallDates
{
        Map mapConIdDate = new Map();
        Map > mapCallAttendees = new Map>();
        Map mapAttcon = new Map();
        List listCon = new List();
        LIst listAttendee = new List();
        List listAllAttendee = new List();
    
        public void getLastCallDate(List triggerNew){
            for(CAll__C oCall: [SELECT id FROM CAll__C WHERE ID IN :TriggerNew])
            {
                mapCallAttendees.put(oCall.id,null);
            }
            for(Attendee__C oAtt : [SELECT ZTS_EU_call__C, id ,ZTS_EU_Contact__C FROM Attendee__C WHERE ZTS_EU_call__c in :TriggerNew])
            {
                listAttendee.add(oAtt);
                System.debug('OATT:;;;;'+oAtt);
                List newList = mapCallAttendees.get(oAtt.ZTS_EU_call__c);
                if(newList == null) newList = new List();
                newList.add(oAtt);
                mapCallAttendees.put(oAtt.ZTS_EU_call__C, newList);
                mapAttCon.put(oAtt.id, oAtt.ZTS_EU_Contact__C );
            }
            System.debug('listAttendee:;;;;'+listAttendee);
            if(listAttendee.size() >0){


                system.debug('mapCallAttendees ::::'+ mapCallAttendees.size());
                listAttendee = [SELECT ZTS_EU_Contact__c FROM Attendee__C WHERE ZTS_EU_Call__c IN :mapCallAttendees.keyset()];
                LIst listConID = new List();
                for(Attendee__c oAtt: listAttendee)
                {
                    listConID.add(OAtt.ZTS_EU_Contact__c);
                }
                listAllAttendee = [SELECT id,ZTS_EU_Contact__c from Attendee__C where ZTS_EU_Contact__c IN : listConID];
                List listAttID =  new List();
                for(Attendee__C oAtt1: listAllAttendee)
                {
                    listAttID.add(oAtt1.id);
                    mapAttCon.put(oAtt1.id, oAtt1.ZTS_EU_Contact__C );
                }
                system.debug('mapAttCon ::::'+ mapAttCon.size());
                //Updated Query to filter only Submitted & Planned Calls - NoorbashaSheik Chanbasha - 11/18/2021 - [SC-008964]
                List results = [SELECT ZTS_EU_Contact__c con,Max(ZTS_EU_call__r.ZTS_EU_Start_Date_Time__c) date from Attendee__c WHERE ID IN: listAttID AND (ZTS_EU_call__r.ZTS_EU_Status__c= 'Submitted' OR ZTS_EU_call__r.ZTS_EU_Status__c='Completed') Group BY ZTS_EU_Contact__c];
                for(AggregateResult ar: results)
                {
                    mapConIdDate.put(String.valueOf(ar.get('con')), Date.valueOF(ar.get('date')));                    
                }
                for(Call__c oCall: triggerNew)
                {
                    for(Attendee__c oAtt : mapCallAttendees.get(oCall.id))
                    {
                        Contact oCon = new Contact();
                        oCon.id = mapAttcon.get(oAtt.id);
                        system.debug('Before assigning call :::'+ oCall.ZTS_EU_Start_Date_Time__c);
                        system.debug('Before assigning :::'+ mapConIdDate.get(oCon.id));
                        if(oCall.ZTS_EU_Start_Date_Time__c > mapConIdDate.get(oCon.id))
                        {
                            system.debug('Before assigning :::'+ mapConIdDate.get(oCon.id));
                            system.debug('call start date :::'+ oCall.ZTS_EU_Start_Date_Time__c);
                            mapConIdDate.put(oCon.ID, Date.valueOf(oCall.ZTS_EU_Start_Date_Time__c));
                            system.debug('After assigning :::'+ mapConIdDate.get(oCon.id));
                        }
                        
                    }
                }
                for(ID conId : mapConIdDate.keyset())
                {
                    system.debug('conId :::'+ conId);
                    Contact oCon = new Contact(id = conId );
                    system.debug('final date :::'+ mapConIDDate.get(conId ));
                    oCon.ZTS_EU_Last_call_Date__c = mapConIDDate.get(conId );
                    system.debug('Print Last date :::'+ oCon.ZTS_EU_Last_call_Date__c);
                    listCon.add(oCon);
                } 
                try {
                    update(listCon);
                } catch(DmlException dex) {
                    String errorMessages = '';
                    for (Integer i = 0; i < dex>                        System.debug('ZTS_EU_Contact_populateCallDates.getLastCallDate ');
                        System.debug('DML Message: ' + dex.getDmlMessage(i));
                        errorMessages += dex.getDmlMessage(i) + 'n';
                    }
                    errorMessages.removeEnd('n');
                    ZTS_EU_Logger.notify = true;
                    ZTS_EU_Logger.error(errorMessages,null, 'ZTS_EU_Contact_populateCallDates.getLastCallDate Contact DML error');
                }
        }
    }
}
Test Class :
@isTest
public class ZTS_EU_Contact_populateCallDatesTest 
{
    static testMethod void validate_ZTS_EU_Contact_populateCallDates() {
        ZTS_DataUtilityTest.createPrimaryContactInactiveProfileCustomSetting(); 
        User usr = new User();
        usr = ZTS_EU_DataUtilityTest.createUserWithProfile('System Administrator', false);
        usr.FirstName = 'davidsUser';
        usr.zts_eu_market__c = 'United Kingdom';
        insert usr;
        System.runAs(usr)
        {
            Map mapConIdDate = new Map();
            Map > mapCallAttendees = new Map>();
            Map mapAttcon = new Map();
            List listCon = new List();
            LIst listAttendee = new List();
            List listAllAttendee = new List();
            ZTS_EU_Contact_populateCallDates popdates= new ZTS_EU_Contact_populateCallDates();
            List contactList=ZTS_EU_DataUtilityTest1.createContactRecord(1);
            contactList[0].ZTS_EU_Market__c = 'United Kingdom';
            insert contactList;
            test.startTest();
            List callList = ZTS_EU_DataUtilityTest1.createCallRecord(1);
            insert callList;
            List attenList=new List();
            //List attenList=ZTS_EU_DataUtilityTest1.createAttendee(1);
            //insert attenList;
            Attendee__c attendee = new Attendee__c();
            attendee.ZTS_EU_Contact__c = contactList[0].Id;
            attendee.ZTS_EU_call__c = callList[0].Id;
            attenList.add(attendee);
            insert attenList;
            //mapCallAttendees.put(callList[0].id,attenList);
            popdates.getLastCAllDate(callList);
            test.stopTest();
        }
    }
}
Answered by Elvera Peasley

We can use validation rules here to get the code coverage caught in test class. Check Contact validation rules which can fire at contact update. Create contact with those values which will be used to fire validation rules. So you can get DML exceptions here.


if you don't have any validation rules to fire at Contact update. try to move these exception block codes to separate methods in the class and use it in the test class. I know it is not a good practice but this is the way to get good test class coverage.

try{
// use any DML and it should fail here
}catch(DMLException ex) {
 // call the new method which will have an exception code.

Your Answer

Interviews

Parent Categories