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
Ramana VRamana V 

Test Class for Inbound Email Service

Hi All,
I have written following inbound email service for lead object.
global class LeadServices implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
    {
        Messaging.InboundEmailResult result = new  Messaging.InboundEmailResult();
        string emailbody = email.plaintextbody;
        string emailsubject = email.subject;
        string subToCompare = emailsubject.Substring(emailsubject.indexof('ref :') + 5).trim();
        try
        {
            lead l = [SELECT
                Id, 
                Name, 
                Email
            FROM
                lead
            WHERE
                id  = : subToCompare];
            // Add a new Task to the lead record we just found above.
            Task newTask = new  Task();
            newTask.Description = emailbody;
            newTask.Priority = 'Normal';
            newTask.Status = 'Inbound Email';
            newTask.Subject = emailsubject;
            newTask.IsReminderSet = true;
            newTask.ReminderDateTime = System.now();
            newTask.WhoId = l.Id;
           
            Insert newTask;
        }
        catch(QueryException e)
        {
            System.debug('Issue: ' + e);
        }
        result.success = true;
        return result;
    }
}
I am have having 55% code coverage now.
Can someone please hepl me with test class for this?

Thanks in Advance

Regards,
Ramana
Best Answer chosen by Ramana V
Deepali KulshresthaDeepali Kulshrestha
Hi Ramana,

I have gone through your problem please refer bellow code:
Inbound email service Class:-

global class LeadServices implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
        {
            Messaging.InboundEmailResult result = new  Messaging.InboundEmailResult();
            string emailbody = email.plaintextbody;
            string emailsubject = email.subject;
            //string subToCompare = emailsubject.Substring(emailsubject.indexof('ref :') + 5).trim();
            try
            {
                lead l = [SELECT
                Id,
                Name,
                Email
                FROM
                lead];
                
                // Add a new Task to the lead record we just found above.
                Task newTask = new  Task();
                newTask.Description = emailbody;
                newTask.Priority = 'Normal';
                newTask.Status = 'Inbound Email';
                newTask.Subject = emailsubject;
                newTask.IsReminderSet = true;
                newTask.ReminderDateTime = System.now();
                newTask.WhoId = l.Id;

                Insert newTask;
            }
            catch(QueryException e)
            {
                System.debug('Issue: ' + e);
            }
            result.success = true;
            return result;
        }
}


Test class:-

@isTest
public class LeadServices_Test {
    @isTest
    private static void testlead(){

        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        Lead l = new lead(firstName='xyz',
                lastName='Abc',
                Company='Salesforce',
                Email='user@acme.com',
                HasOptedOutOfEmail=false);
        insert l;
        System.debug(l);
        email.subject = 'Create Lead';
        email.fromAddress = 'someaddress@email.com';

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

    }

}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ramana,

Greetings to you!

You are using emailsubject.Substring(emailsubject.indexof('ref :') + 5).trim(); as an Id to get the lead record which is not a valid id. 

Please refer to the below links which might help you further with the above requirement.

http://amitsalesforce.blogspot.com/2016/12/test-class-for-email-service-test-class.html

http://www.sfdcmeet.com/development/inbound-email-service/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Ramana,

I have gone through your problem please refer bellow code:
Inbound email service Class:-

global class LeadServices implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
        {
            Messaging.InboundEmailResult result = new  Messaging.InboundEmailResult();
            string emailbody = email.plaintextbody;
            string emailsubject = email.subject;
            //string subToCompare = emailsubject.Substring(emailsubject.indexof('ref :') + 5).trim();
            try
            {
                lead l = [SELECT
                Id,
                Name,
                Email
                FROM
                lead];
                
                // Add a new Task to the lead record we just found above.
                Task newTask = new  Task();
                newTask.Description = emailbody;
                newTask.Priority = 'Normal';
                newTask.Status = 'Inbound Email';
                newTask.Subject = emailsubject;
                newTask.IsReminderSet = true;
                newTask.ReminderDateTime = System.now();
                newTask.WhoId = l.Id;

                Insert newTask;
            }
            catch(QueryException e)
            {
                System.debug('Issue: ' + e);
            }
            result.success = true;
            return result;
        }
}


Test class:-

@isTest
public class LeadServices_Test {
    @isTest
    private static void testlead(){

        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        Lead l = new lead(firstName='xyz',
                lastName='Abc',
                Company='Salesforce',
                Email='user@acme.com',
                HasOptedOutOfEmail=false);
        insert l;
        System.debug(l);
        email.subject = 'Create Lead';
        email.fromAddress = 'someaddress@email.com';

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

    }

}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer