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 Messaging.InboundEmailHandler class

I'm having a bit of trouble writing a test class for the Messaging.InboundEmailHandler class.  I am stuck at 44%.  Any suggestions on how I can achieve 100% code coverage would be greatly appreciated.

Apex Class
// Apex class used to capture reply emails from clients and attach 
// to their lead record
// A task is created against the lead record with the 
// contents of the reply email
// A workflow has also been created to notify the lead
// owner that a new inbound email has been created
global class ProcessReplyEmails implements Messaging.InboundEmailHandler {
    
    Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env ) {
        
        // Create an inboundEmailResult object for returning 
        // the result of the email service.
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        
        String mySubject = email.subject;
        String myPlainText = '';
        myPlainText = email.plainTextBody;
        
        // Capture the record ID from the subject line
        String refID = mySubject.right(15);
        System.debug('extract Subject: ' + refID);
        
        // New email task to be created
        Task[] newTask = new Task[0];
        try
        {
        	// Try to lookup the lead record that matches the ID in the subject
        	Lead l = [SELECT Id, Name, Email, Company, Phone, OwnerID FROM Lead WHERE ID = :refID];
            
            newTask.add(new Task(Description = myPlainText,
                                	Priority = 'Normal',
                                	Status = 'Inbound Email', 
                                	Subject = mySubject,
                                	whoId = l.Id
                                ));
            // insert the new task
            insert newTask;
            System.debug('New Task Created:' +newTask);
            
                if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) 
                {
                    for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) 
                    {
                        Attachment attachment = new Attachment();
                        // attach to the found record
                        attachment.ParentId = l.Id;
                        attachment.Name = email.binaryAttachments[i].filename;
                        attachment.Body = email.binaryAttachments[i].body;
                        attachment.Description = mySubject;
                        insert attachment;
                    }
                }                
        }
        catch(Exception e)
        {
            System.debug('Exception Issue: ' +e);
        }

        // Return True and exit.
        // True confirms program is complete and no emails 
        // should be sent to the sender of the reply email. 
        result.success = true;
        return result;
    } 
}

Apex Test Class
@isTest(SeeAllData=true)
public class ProcessReplyEmailsTest {
 public static testMethod void testProcessReplyEmails() 
    {        
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        email.subject = 'Test Subject ';
        email.fromAddress = 'xxx@xxx.com';
        email.plainTextBody = 'Blah Blah Blah';        
          
        ProcessReplyEmails testProcessReplyEmails = new ProcessReplyEmails();
        testProcessReplyEmails.handleInboundEmail(email, env);

             
    }
}

The lines not covered start at newTask.add.

Thanks!
Best Answer chosen by Christine_K
Mahesh DMahesh D
In your test class you are appending the l.Id which will give you 18 digit SFDC id.

Please change the code at line 20 to below and verify the result:

String refID = mySubject.right(18);

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Christine_Klein,

Please create the Lead record in your test class and append lead id to email.subject as it is expecting the right(15).

Regards,
Mahesh
Christine_KChristine_K
Thanks Mahesh.  I had that in my first attempt but the code coverage never changed from 44%
Mahesh DMahesh D
Could you please post your latest test class code because the code above it shows

email.subject = 'Test Subject ';

 
Christine_KChristine_K
@isTest(SeeAllData=true)
public class ProcessReplyEmailsTest {
 public static testMethod void testProcessReplyEmails() 
    {        
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        Lead l = new Lead();
        l.firstname = 'Test';
        l.lastname = 'Test';
		l.email = 'test@test.com';
        l.Phone = '123-456-7897';
        l.Company = 'Test Company';
        l.Status = 'New';
        insert l;        

        email.subject = 'Test Subject Reference # ' + l.id ;
        email.fromAddress = l.email;
        email.plainTextBody = 'Blah Blah Blah';        
          
        ProcessReplyEmails testProcessReplyEmails = new ProcessReplyEmails();
        testProcessReplyEmails.handleInboundEmail(email, env); 
    }
}

 
sumit singh 37sumit singh 37
static testMethod void testMe() {

Lead l = new Lead();
        l.firstname = 'Test';
        l.lastname = 'Test';
		l.email = 'test@test.com';
        l.Phone = '123-456-7897';
        l.Company = 'Test Company';
        l.Status = 'New';
        insert l;        


  // create a new email and envelope object
  Messaging.InboundEmail email = new Messaging.InboundEmail() ;
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

  // setup the data for the email
  email.subject = 'Test Subject Reference # ' + l.id;
  email.fromname = 'FirstName LastName';
  env.fromAddress = l.email;

  // add an attachment
  Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfile.txt';
  attachment.mimeTypeSubType = 'text/plain';

  email.binaryAttachments =
    new Messaging.inboundEmail.BinaryAttachment[] { attachment };

  // call the email service class and test it with the data in the testMethod
  ProcessReplyEmails  emailProcess = new ProcessReplyEmails ();
  emailProcess.handleInboundEmail(email, env);

  // query for the Task the email service created
  Task tsk = [select id from Task where whoid=l.id];

  

  // find the attachment
  Attachment a = [select name from attachment where parentId = :l.id];

  

}

 
Mahesh DMahesh D
In your test class you are appending the l.Id which will give you 18 digit SFDC id.

Please change the code at line 20 to below and verify the result:

String refID = mySubject.right(18);

Regards,
Mahesh
This was selected as the best answer
Christine_KChristine_K
Thanks Mahesh - the 18 digit ID was the problem!