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
Krish NKrish N 

Help needed for Apex Emailservices Inbound Email handler test class

Hello, I need help in writing test class for the below InboundemailHandler. The functionality is we get automated emails in a particular format for service requests. Incoming email will be parsed through Apex email services and a case will be created. The Apex class is working very well, but I'm not sure how to write the test class. Can any one help me? Thank you!
** This is how the incoming email looks like**

Service Request #: 1-3747513422

RFS State: Waiting for ETA

Vendor: Sample vendor

Vendor Phone: (222) 943-1601

Trade: Sample Equipment

Not to Exceed: $300

Service Need: Replacment

Service Level: Critical - P1

Root Cause: Call Back

Service Requested: 07/26/2018 02:58 PM

Required Arrival Time: 07/26/2018 06:58 PM

Target Completion Date: 07/27/2018 02:58 AM

Email Delivery Time: 07/26/2018 02:58 PM

Client: Test client #230

Address: 1490 S Main
Jefferson, CA 11111

RFS Initiator Name: Test Manager

Client Contact Phone: (111) 451-1686

Client Contact Fax: 

Store Contact: 333-928-3276

Store Number: 230
 
global Class EtoCaseservices implements Messaging.InboundEmailHandler {

// Instantiate variables that we will need for handling this email
   
String location;
Integer locationIdx;
String locationFinal;
String need;
String Inbox;
String initiator;
String Time;
String po;
String phone;
String order;
String critical;


global Messaging.InboundEmailResult handleInboundEmail
           (Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){  
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 if (email.fromaddress =='test@gmail.com')
               {
                   
                   
loc = 'Client: ';   
locationIdx = email.plainTextBody.indexOf(location);
String[] bodySplitted = email.PlainTextBody.split('\n');
locationFinal = email.plainTextBody.substring(
                              locationIdx + location.length(), email.plainTextBody.indexOf('\n', locationIdx + location.length()));
String[] locNme = locationFinal.split(' ', 3);
phone = email.plainTextBody.substringBetween('Client Contact Phone: ' , 'Client Contact Fax: ');
Inbox = email.plainTextBody.substringBetween('Email Delivery Time: ', 'Client: ');                
initiator = email.plainTextBody.substringBetween('RFS Initiator Name: ' , 'Client Contact Phone: ');          
Time = email.plainTextBody.substringBetween('Service Requested: ' , 'Email Delivery Time: ');
po = email.plainTextBody.substringBetween('Service Request #: ' , 'RFS State: ');                   
critical = email.plaintextbody.substringBetween('Service Level: ' , 'Root Cause: ');
need = email.plaintextbody.substringBetween('Service Need: ' , 'Service Level: ');
order = email.plaintextbody.substringBetween('Not to Exceed: ' , 'Service Need: '); 
system.debug('locationFinal: ' +locationFinal);
if (locationFinal != NULL) {
    SVMXC__Site__c [] locArray = [Select Id, Name, SVMXC__Account__r.Id from SVMXC__Site__c where Name = :locationFinal];
           
      try{                   
case c= new case();
c.subject= 'TEST REQUEST';
c.Case_Type__c= 'Service Request';
c.Origin='Email';
c.Status='new';
c.AccountId = locArray[0].SVMXC__Account__r.Id;
c.SVMXC__Site__c = locArray[0].Id;  
c.recordtypeId = '012E0000000oRWX';                
c.Description= critical +  '\n' +  need + '\n' + orderFinal + '\n'  + timeFinal ;
c.KA_PO__c= po;
c.Location_Contact_Name__c = Initiator; 
c.BSP_Location_Contact_Phone__c = phone;
insert c; 
}
catch(System.dmlException e)
     {System.debug('Error: Unable to create new Case: ' + e);
     }         
                    }
        else if (locationFinal == null){
              System.debug('No Location was found');    
        }
      
    }
              
return result;

      } // Close handleInboundEmail ()
}
Raj VakatiRaj Vakati
Try this class  .. you need to make sure set the data email and env (InboundEmail  ,InboundEnvelope)
 
@isTest
private class EtoCaseservicesTest {
    static testMethod void testCaseFromEmail() {
        
        // 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 Email Subject';
        email.fromAddress='test@gmail.com';
        email.plainTextBody='plainTextBodylocation \n Client Contact Phone:12312739982 \n Email Delivery Time: 12319236 Service Request #:123123123 Service Level:123123';
        
        env.fromAddress = 'sbrady@digitalhands.com';
        
        // 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 };
                
                
                EtoCaseservices emailHandler = new EtoCaseservices();
        emailHandler.handleInboundEmail(email, env);
        
        
        // Make assertions about the case that should be created.
    }
}

 
Raj VakatiRaj Vakati
@isTest
private class EtoCaseservicesTest {
    static testMethod void testCaseFromEmail() {
        
		// insert account 
		// inser contact 
		//insert SVMXC__Site__c 
		
        // 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 Email Subject';
        email.fromAddress='test@gmail.com';
        email.plainTextBody='plainTextBodylocation \n Client Contact Phone:12312739982 \n Email Delivery Time: 12319236 Service Request #:123123123 Service Level:123123';
        
        env.fromAddress = 'sbrady@digitalhands.com';
        
        // 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 };
                
                
                EtoCaseservices emailHandler = new EtoCaseservices();
        emailHandler.handleInboundEmail(email, env);
        
        
        // Make assertions about the case that should be created.
    }
}

 
Krish NKrish N
Thank you Raj. I will give it a try. Shouldn't the whole email body be inserted in  email.plainTextBody=     ; ?
Raj VakatiRaj Vakati
set email.plainTextBody .. that will enough