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
Rocks_SFDCRocks_SFDC 

How to Write Test Class for Email Service Class

Hello everyone,

 

I have one class on Email Services. Could please any one let me know how to write Test Class for it.

 

I am Pasting my code below:

 

global class CreateTaskfromLeadBox implements Messaging.InboundEmailHandler 
{
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
  {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String myPlainText= '';
    myPlainText = email.plainTextBody;
    Task[] newTask = new Task[0];   
    
    try 
    {
          Lead le= [SELECT Id, Name, Email,ownerId FROM Lead WHERE Email = :email.toAddresses LIMIT 1];
          if(le!=null)
          {
          
          newTask.add(new Task(Description =  myPlainText,
               Priority = 'Normal',
               Status = 'Completed',
               Subject = email.subject,
               ownerid =le.ownerId,
               WhoId =  le.Id));
          insert newTask;
          }
    }
    catch (QueryException e) 
    {
       System.debug('Query Issue: ' + e);
    }
   result.success = true; 
   return result;
  }
}

 

 

 

Thanks,

Anil

 

 

 

Rocks_SFDCRocks_SFDC

Thanks Amit.

 

I have written the test class.

 

@isTest
public Class CreateTaskfromLeadBoxTest{
public static testMethod void CreateTaskfromLead(){

// Create a new email and envelope object
        Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        // Create the email body
        email.plainTextBody = 'This should become a note';
        email.fromAddress ='test@test.com';
        String contactEmail = 'jsmith@salesforce.com';
        email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
        email.subject = 'Dummy Account Name 123';
        
        
        CreateTaskfromLeadBox edr = new CreateTaskfromLeadBox ();
        
       Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
        edr.handleInboundEmail(email,env);

 

        }

 

}

 

when i run above test class, i am getting the following error.

 

Attempt to de-reference a null object..

 

This error i am getting at the line which i marked in red colour.

 

Thanks,

Anil

Rocks_SFDCRocks_SFDC

Hello Everyone,

 

Do we have any update on this???

 

Thanks,

Anil