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 

Test class failing for Apex Email services

Hello everyone, I'm having trouble writing the test class for email services class. 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 unable to succeed in test class. I've looked at various documents and resources online, but it only increased my confusion. Can any one please 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

Below is the Apex class, it's working fine with no errors.
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 ()
}

Please check the test class. Resulting in "System.QueryException: List has no rows for assignment to SObject" error.
@istest

public class TestEtoCaseservices {
    static testMethod void TestEtoCaseservices () {

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


    Account acc = new Account();
    acc.Name = 'Test Smith';
    insert acc;

    SVMXC__Site__c l = new SVMXC__Site__c(Name='Test Loc', SVMXC__City__c='Detroit', NTT_State__c='MI', SVMXC__Site_Phone__c='313 111 1231', SVMXC__Country__c='United States', NTT_Metro_Remote__c='ZONE A');
    insert l;

       // setup the data for the email
     email.subject = 'Test Loc';
     env.fromAddress = 'someaddress@email.com';
     email.plainTextBody = 'This is just a test email for test class';


      // call the email service class and test it with the data in the testMethod
      EtoCaseservices  testInbound=new EtoCaseservices ();
      testInbound.handleInboundEmail(email, env);
    Messaging.InboundEmailResult result = testInbound.handleInboundEmail(email, env);
System.assertEquals( result.success  ,true);

Case c = [select id, account.id, SVMXC__Site__c, recordtypeId, KA_PO__c, Location_Contact_Name__c, description, BSP_Location_Contact_Phone__c, subject, Case_Type__c, Origin, Status, AccountId from Case where SVMXC__Site__c = :l.Id];

System.assertEquals(c.SVMXC__Site__c, l.Id);
System.assertEquals(c.account.Id, acc.Id);
System.assertEquals(c.recordtypeId, '012E0000000oRWX');
System.assertEquals(c.KA_PO__c, '10010101');
System.assertEquals(c.Location_Contact_Name__c, 'Test Man');       
System.assertEquals(c.BSP_Location_Contact_Phone__c, '111 222 3443');
System.assertEquals(c.subject, 'Test Request');
System.assertEquals(c.Case_Type__c, 'Service Test'); 
System.assertEquals(c.Origin, 'Email');
System.assertEquals(c.Status, 'New');  
System.assertEquals(c.description, 'Test description');  
update c;
    }     

}


 
Raj VakatiRaj Vakati
Change it as below 


set the subject of the test class ..... and add the all values 


     email.plainTextBody = 'This is just a test email for test class . Client:Test Loc \n Client Contact Phone:12312323 ';



Remove recordtypeId from the hardcoded values
 
@istest

public class TestEtoCaseservices {
    static testMethod void TestEtoCaseservices () {

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


    Account acc = new Account();
    acc.Name = 'Test Smith';
    insert acc;

    SVMXC__Site__c l = new SVMXC__Site__c(Name='Test Loc', SVMXC__City__c='Detroit', NTT_State__c='MI', SVMXC__Site_Phone__c='313 111 1231', SVMXC__Country__c='United States', NTT_Metro_Remote__c='ZONE A');
    insert l;

       // setup the data for the email
     email.subject = 'Test Loc';
     env.fromAddress = 'someaddress@email.com';
//Set Subject to meet all conditions 

     email.plainTextBody = 'This is just a test email for test class . Client:Test Loc \n Client Contact Phone:12312323 ';


      // call the email service class and test it with the data in the testMethod
      EtoCaseservices  testInbound=new EtoCaseservices ();
      testInbound.handleInboundEmail(email, env);
    Messaging.InboundEmailResult result = testInbound.handleInboundEmail(email, env);
System.assertEquals( result.success  ,true);

Case c = [select id, account.id, SVMXC__Site__c, recordtypeId, KA_PO__c, Location_Contact_Name__c, description, BSP_Location_Contact_Phone__c, subject, Case_Type__c, Origin, Status, AccountId from Case 
where SVMXC__Site__c = :l.Id];

System.assertEquals(c.SVMXC__Site__c, l.Id);
System.assertEquals(c.account.Id, acc.Id);
//remove this hard coded values and pass by quering 
System.assertEquals(c.recordtypeId, '012E0000000oRWX');
System.assertEquals(c.KA_PO__c, '10010101');
System.assertEquals(c.Location_Contact_Name__c, 'Test Man');       
System.assertEquals(c.BSP_Location_Contact_Phone__c, '111 222 3443');
System.assertEquals(c.subject, 'Test Request');
System.assertEquals(c.Case_Type__c, 'Service Test'); 
System.assertEquals(c.Origin, 'Email');
System.assertEquals(c.Status, 'New');  
System.assertEquals(c.description, 'Test description');  
update c;
    }     

}

 
Krish NKrish N
Hello Raj, I did add the content to plaintextbody but it still the same. Test class is failing with List has no rows to for assignment to sobject error. I really have no idea where I'm missing out. 
email.subject = 'Test email subject';
     env.fromAddress = 'someaddress@email.com';
     email.plainTextBody = 
         
'Service Request #: 10010101'+ '\n' +

'RFS State: Waiting for ETA'+ '\n' +

'Vendor: Sample Service, LLC (SEI)'+ '\n' +

'Vendor Phone: (11) 222-1601'+ '\n' +

'Trade: Cold Beverage Equipment'+ '\n' +

'Not to Exceed: $300'+ '\n' +

'Service Need: Ice Maker - Not Dispensing Ice'+ '\n' +

'Service Level: Critical - P1'+ '\n' +

'Root Cause: Call Back'+ '\n' +

'Service Requested: 07/26/2018 02:58 PM'+ '\n' +

'Required Arrival Time: 07/26/2018 06:58 PM'+ '\n' +

'Target Completion Date: 07/27/2018 02:58 AM'+ '\n' +

'Email Delivery Time: 07/26/2018 02:58 PM'+ '\n' +

'Client: Test Loc'+ '\n' +

'Address: 1410 S Main St , MI 97355'+ '\n' +

'RFS Initiator Name: Test Man'+ '\n' +

'Client Contact Phone: 111 222 3443'+ '\n' +

'Client Contact Fax: '+ '\n' +

'Vendor Contact: 888-928-3276'+ '\n' +

'Store Number: 1111';
Krish NKrish N
Any help guys?