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
Christine_KChristine_K 

Code Coverage for Trigger.IsDelete

I was hoping to get some assistance with code coverage for a particular class.

In my trigger I have:
else if(Trigger.isDelete)
    {
        handler.clearAccountEmails(Trigger.old, Trigger.new, Trigger.oldMap);
    }
Trigger Handler:
public void clearAccountEmails(Contact[] oldContacts, Contact[] updatedContacts, Map<ID, Contact> oldContactsMap)
    {
        List<Account> accounts = new List<Account>();          
        for(Contact c : oldContacts)
        {
            if(c.AccountId != null && c.Primary_Contact__c)
            {
                Account a = new Account(id = c.AccountId);
                if(c.Email != null) a.Primary_Contact_Email_1__c = '';
                if(c.Email != null) a.Customer_Email__c = '';
                if(c.Email_2__c != null) a.Primary_Contact_Email_2__c = '';
                if(c.Servicing_Contact_Method__c  != null) a.Servicing_Contact_Method__c  = ''; 
                accounts.add(a);
            }  
        }
        
        if(!accounts.isEmpty())
        {
            try{
                update accounts;
            }
            catch(exception e){
                System.debug('The following exception has occurred: ' + e.getMessage());
            }
            system.debug(accounts);
        }

I can't seem to get code coverage for the above.  Any assistance would be greatly appreciated.

Thanks!

 
Raj VakatiRaj Vakati
Try some think below 
 
@isTest
private class TestAContactTrigger {
  static testmethod void deleetTest(){
	   Account a = new Account();
       a.Name = 'test';
          insert a;
  
  
       Contact c =new Contact();
        c.lastName = a.Name;
        c.Phone = a.phone;
        c.AccountId = a.id;
		insert c;
		
		
		delete c;
		
		

}
}

 
Christine_KChristine_K
That's what I did for my first attempt at obtaining code coverage for the IsDelete, but that doesn't seem to do the trick.  Lines still come back in red.
Raj VakatiRaj Vakati
Can you share the complete trigger?
Christine_KChristine_K
trigger Contact on Contact (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    
    ContactTriggerHandler handler = new ContactTriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if(Trigger.IsBefore)
    {
        if(!Trigger.isDelete)
        {  
            handler.updateAccountEmails(Trigger.old, Trigger.new, Trigger.oldMap);       
            handler.updatePreferences(Trigger.old, Trigger.new, Trigger.oldMap);
            //handler.UpdateContacts(Trigger.old, Trigger.new, Trigger.oldMap);
        }
    }
    else if(Trigger.IsInsert && Trigger.IsUpdate){
        if(!Trigger.isDelete)
        { 
            handler.updateAccountEmails(Trigger.old, Trigger.new, Trigger.oldMap);
            //handler.updatePreferences(Trigger.old, Trigger.new, Trigger.oldMap);
        }
    } 
    else if(Trigger.IsAfter)
    {
        if(!Trigger.isDelete)
        { 
            handler.updateAccountEmails(Trigger.old, Trigger.new, Trigger.oldMap);
        }
    }     
    else if(Trigger.isDelete)
    {
        handler.clearAccountEmails(Trigger.old, Trigger.new, Trigger.oldMap);
    }    
}

 
Christine_KChristine_K
I just moved the Trigger.IsDelete above the isAfter and that resolved the issue.