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
StaciStaci 

test class coverage for attachments in email-to-case

I have the following code to move attachments from the inbound email message into Attachments - we are still in classic

I have 53% coverage right now.  Can't figure out how to get the last half of this code covered.
//test coverage in DSSEmailHandlerTest 
trigger CW_emailAttachmentReassigner on Attachment (before insert) {
        map<id, id> msgIdToParentId = new map<id, id>();
        Attachment[] reparents = new Attachment[]{};
        Schema.sObjectType email = emailmessage.getsobjecttype();
    

        for(Attachment a : trigger.new) {
            if(a.parentid != null){
                //see if the parent is an EmailMessage
                if(a.parentid.getsobjecttype() == EmailMessage.getsObjectType()) {



 //Test code from here down                 
 msgIdToParentId.put(a.parentid, null);
                    reparents.add(a);
                }
            }
        }
        
        if(!reparents.isEmpty()){
            for(EmailMessage em : [select id, parentID from EmailMessage where id =: msgIdToParentId.keyset()]){
                msgIdToParentId.put(em.id, em.parentId);
            }
        
            for(Attachment a : reparents) {
                a.parentId = msgIdToParentId.get(a.parentId);
            }
        }
        
    }
 
@isTest
private class DSSEmailHandlerTest
{
public static Blob createAttachmentBody(){
        String body = 'XXXXXXXXXXXXX';
        return Blob.valueof(body);
    }
    public static testMethod void testmyHandlerTest() 
    {
        Case c = new Case();
        insert c;
        
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        email.subject = 'ref';
        email.PlainTextBody = 'test body';
        
        // Add a Binary attachment to the email message!
        Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
        attachment.body = blob.valueOf('my attachment content');
        attachment.fileName = 'filename.txt';
        attachment.mimeTypeSubType = 'text/plain';
        email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
        
        DSSEmailHandler m = new DSSEmailHandler();
        m.handleInboundEmail(email, env);
     }
     
     
}

 
Best Answer chosen by Staci
AnkaiahAnkaiah (Salesforce Developers) 
Hi Staci,

try with below code. You will get 100% coverage.
 
@isTest 
public class DSSEmailHandlerTest {
    static testMethod void testMethod1(){
 Case c = new Case();
        c.Subject = 'Om Test';  
        c.Status ='New';
        c.Priority = 'Medium';
        c.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;
        
        Attachment attach=new Attachment();       
         attach.Name='Unit Test Attachment';
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
          attach.body = bodyBlob;
          attach.parentId = email.id;
        insert attach;
        
        }
}

If this helps, Please mark it as best answer.

Thanks!!

 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Staci,

try with below code. You will get 100% coverage.
 
@isTest 
public class DSSEmailHandlerTest {
    static testMethod void testMethod1(){
 Case c = new Case();
        c.Subject = 'Om Test';  
        c.Status ='New';
        c.Priority = 'Medium';
        c.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;
        
        Attachment attach=new Attachment();       
         attach.Name='Unit Test Attachment';
         Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
          attach.body = bodyBlob;
          attach.parentId = email.id;
        insert attach;
        
        }
}

If this helps, Please mark it as best answer.

Thanks!!

 
This was selected as the best answer
StaciStaci
thanks @ankaiah!