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

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 () }
Try this class .. you need to make sure set the data email and env (InboundEmail ,InboundEnvelope)

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