0

I wrote some code to update the Initial Response field on a case when an outbound email is sent. I can't seem to get enough coverage to deploy the trigger handler. Here's my code

Trigger

trigger EmailMessageTrigger on EmailMessage (after insert){
System.debug('Trigger Settings' + TriggerSettings__c.getInstance('EmailMessage'));
if (TriggerSettings__c.getInstance('EmailMessage') == null || 
    TriggerSettings__c.getInstance('EmailMessage').Disable_Triggers__c == null || 
    TriggerSettings__c.getInstance('EmailMessage').Disable_Triggers__c == false) {
    EmailMessageTriggerHandler.handleOutboundCaseEmails();
}
}

EmailMessageTriggerHandler

public class EmailMessageTriggerHandler {

public static Boolean isPortal = ([Select ContactId from User where Id =:UserInfo.getUserId()].ContactId==null?false:true);
public static void handleOutboundCaseEmails(){
    
    Map<Id,Case> mapUpdates = new Map<Id,Case>();
    List<ID> CaseID = New List<ID>(); 
    List<Case> caseToUpdate = new List<Case>();
    for(EmailMessage em :(List<EmailMessage>)Trigger.new){
        System.debug(em);
        if(em.Incoming == false){
            if(em.ParentId != null){
                if(em.ParentId.getSObjectType() == Case.SObjectType){
                    CaseID.add(em.ParentId);
                    if(!isPortal){
                        mapUpdates.put(em.ParentId, new Case(Id=em.ParentId,Waiting_on_Customer__c=true));
                    }
                    System.debug('Case ID: ' + CaseID);
                    List<Case> caseList = [Select Id, Initial_Response__c FROM Case Where Id = :CaseID];
                    System.debug('Case List: '+ caseList);
                    if(!caseList.isEmpty()){    
                        for(Case c : caseList){
                            ID cas = c.Id; 
                            System.debug(cas);
                            AggregateResult[] initialResponseDate = [Select Min (CreatedDate) initialResponseDate from EmailMessage where ParentId = :cas And Incoming = false];
                            System.debug(initialResponseDate);    
                            c.Initial_Response__c = date.valueOf(initialResponseDate[0].get('initialResponseDate'));
                            System.debug(c.Initial_Response__c);
                            caseToUpdate.add(c);
                        }
                        System.debug(caseToUpdate);
                        update caseToUpdate;
                    }
                }
            }
        }
    }
        update mapUpdates.values();

 }

}

My trigger is getting 50% coverage where the two lines above handleOutboundCaseEmails() is not getting any coverage and the triggerhandler coverage is at 38% where code below if(em.Incoming == false) is not getting any coverage. How can I achieve at least 75% coverage so that I can deploy my code?

Test Class

    @isTest static void Outbound() {
    TriggerSettings__c triggerSet = new TriggerSettings__c();
    triggerSet.Name = 'EmailMessage';
    triggerSet.Disable_Triggers__c = false;
    insert triggerSet;
    EmailMessage message = new EmailMessage();
    message.ToAddress = 'lbejjani@fuze.com';
    message.Subject = 'Test';
    message.TextBody = 'Testing';
    message.Incoming = false;
    
    message.ParentId = sc.Id;
    //message.ParentId = '5006s000000hGHZAA2';
    insert message;
}  
1
  • This is a "smoke test" - it validates only that the code does not throw an exception. Please consider writing one or more meaningful assertions to validate that your code does what you expect it to.
    – David Reed
    Nov 10, 2020 at 5:11

2 Answers 2

0

In general, I'd suggest creating private functions which handle logic. The use @TestVisible to run unit tests for each of them in isolation. A deeply nested method as in your EmailMessageTriggerHandler becomes hard to effectively test & assert upon. It should be relatively easy to reach 75%. But that should not be your guiding line. Your guiding line should be assertable tests.

Also, I'd not recommend putting SOQL inside your loops. This is very tricky code as it seems not bulkified-proof.

2
  • I have been able to increase test coverage to 100% on the trigger and 71% on the trigger handler. In the code I insert a case and return sc then I try to set the email message's ParentId to sc.Id when I do that I get attempt to de-reference a null object. When I haarcode the ParentId I get the 71% coverage. @Jeronimo - How do I bulkify this code? I edited my question to include the test class. Nov 9, 2020 at 18:00
  • Hi @LauraBejjani Trailhead is your best buddy here: trailhead.salesforce.com/content/learn/modules/apex_triggers/…
    – Jeronimo
    Nov 10, 2020 at 9:53
0

I was finally able to achieve 100% coverage on both classes with the following test method

    @isTest static void Outbound() {
    ID caseId = InsertSupportCase().Id;
    TriggerSettings__c triggerSet = new TriggerSettings__c();
    triggerSet.Name = 'EmailMessage';
    triggerSet.Disable_Triggers__c = false;
    insert triggerSet;
    EmailMessage message = new EmailMessage();
    message.ToAddress = 'test@test.com';
    message.Subject = 'Test';
    message.TextBody = 'Testing';
    message.Incoming = false;
    
    message.ParentId = caseId;
    //message.ParentId = '5006s000000hGHZAA2';
    insert message;
}  

Not the answer you're looking for? Browse other questions tagged or ask your own question.