You need to sign in to do that
Don't have an account?

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 Test Class
The lines not covered start at newTask.add.
Thanks!
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!
Please change the code at line 20 to below and verify the result:
String refID = mySubject.right(18);
Regards,
Mahesh
All Answers
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
email.subject = 'Test Subject ';
Please change the code at line 20 to below and verify the result:
String refID = mySubject.right(18);
Regards,
Mahesh