0

I have created 2 triggers to save a custom field on Case - Description_HTML__c which should keep the HTML version of the Case Description.

The first trigger is:

trigger updateCaseDescription on Case (before update) {
    set<Id> parentIds = new set<Id>();
    map<Id,EmailMessage> mapEmailMessage = new map<Id,EmailMessage>();
    for(Case c:trigger.new){   
        parentIds.add(c.Id);
    }   
    list<EmailMessage> lste = [select id,HtmlBody,parentId from EmailMessage where parentid in:parentIds and Incoming = true];

    if(lste.size() > 0 ){
     for(EmailMessage e:lste){
         mapEmailMessage.put(e.parentId,e);
     }
     list<Case> lstC = new list<Case>();
     for(Case c:trigger.new){ 
      if(mapEmailMessage != null && mapEmailMessage.get(c.Id) != null)   
          c.Description_HTML__c = mapEmailMessage.get(c.Id).HtmlBody;       
     }
    }
}

And the test class is:

@isTest
private class updateCaseDescriptionTest {

    static testMethod void myUnitTest() {
        Case c = new Case();
        c.Subject = 'Om Test';  
        c.Description_HTML__c = 'testing';
        EmailMessage b = new EmailMessage();
        b.HtmlBody = 'test';

        try
        {
            insert c;
        }
        catch(System.DMLException e)
        {
            System.assert(e.getMessage().contains('Record already exist with same email Id'));
        }
    }
}

My Code coverage yields 50%. How can I improve that? (targeting 75%).

The second Trigger is destined to reveal the inline images in the 'Description_HTML__c' custom field, and is as follows:

trigger UpdateDescTrigger on EmailMessage (after insert,after update,before insert ,before update) {
    //set<id> CaseIds = new set<Id>();
    map<Id,EmailMessage> MapEmailMessage = new map<Id,EmailMessage>();
    List<Case> newList = new List<Case>();
    for(EmailMessage e:trigger.new){
        MapEmailMessage.put(e.ParentId,e);
    }

    list<Case> lstC = [select Id,Description_HTML__c from Case where id in:MapEmailMessage.keyset()];
    for(Case c:lstC){
        if(MapEmailMessage != null && MapEmailMessage.get(c.Id) != null )
            c.Description_HTML__c = MapEmailMessage.get(c.Id).HtmlBody;
            newList.add(c);
    }
    if(newList.size()>0)
    {
        update newList;
    }
}

For this second trigger I am unsure where to begin with the test class.

  • 1
    1st test - are you inserting the EmailMessage recording? 2nd Test - Create a Case Record, then create and insert an EmailMessage where the ParentId = Case.Id. Plus lots of system asserts :) – Girbot Feb 8 '17 at 20:31
  • 1
    A note - the assert in your catch will not prevent the test from succeeding if the catch never occurs. I would save the exception's message to a String and place your assert outside of the try-catch block. – SFDC Neuf Feb 8 '17 at 23:03
1

The reason for minimal code coverage of trigger1 and no code coverage of trigger2 is you are not inserting any test records for EmailMessage. Try this test class:

@isTest
private class updateCaseDescriptionTest {

    static testMethod void myUnitTest() {
        //Insert test case record
        Case c = new Case();
        c.Subject = 'Om Test';  
        c.Description_HTML__c = 'testing';
        Status ='New';
        Priority = 'Medium';
        Origin = 'Email';
        insert c;

        //Insert emailmessage for case
        EmailMessage email = new EmailMessage();
        email.FromAddress = 'test@abc.org';
        email.Incoming = True;
        email.ToAddress= 'test@xyz.org';
        email.Subject = 'Test email';
        email.HtmlBody = 'Test email body';
        email.ParentId = c.Id; 
        insert email;

        //Assert if case Description_HTML__c updated with emailmessage htmlbody value
        System.assertEquals([Select Id, Description_HTML__c From Case Where Id = :c.Id][0].Description_HTML__c,
                            'Test email body');
    }
}

If you need to assert any exception, try to insert a case record with field values that may cause exception.

  • Thanks Reshma. This test covers 100% of the trigger. I'm facing a different issue now - this trigger is actually updating the 'Description_HTML__c' custom field of the Case object, on EVERY EmailMessage which arrives in the Case. Originally, I wanted to update the 'Description_HTML__c' only once, with the first incoming Email Message. Any change you can further help me adjust the SOQL query to meet this functionality? – iDog Feb 9 '17 at 8:10
  • @iDog This is something different requirement than the question you posted. Please post it as a new one and accept the answer if it resolved your issue. – Reshma Feb 9 '17 at 8:33
  • Thank you for your help Reshman. Much Appreciated. I reported the additional issue as a new question: salesforce.stackexchange.com/questions/159730/… – iDog Feb 9 '17 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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