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 

Question regarding Apex Email services for email to cases

Hello, Our requirement is to create cases from incoming emails. We have around 25 customers and each of them have a specific service request template email format. We have to parse the incoming mail and create case and assign the values to fields. I know how to create the Apex class. I've tested for two customers and it's working great.  
My question is how do I create an Apex class for all the 25 templates at once. Is that even possible? All these emails come to the same email address. I cannot create multiple email services/classes  (as the forwarding address can only be one).This is my current class. I'm using "if else" based on the incoming email address and parsing the data. But how do I do it for 25 email addresses? I'm new to Apex and having a hard time dealing with it. Please help. This code is working fine, but it only deals with two customers. How do I do it for 25 customers?
global Class EtoCaseservices implements Messaging.InboundEmailHandler {

// Instantiate variables that we will need for handling this email
   
String location;
String need;
String service;
String phone;
String warranty;
String po;
String description;
String initiator;
String order;
Integer locationIdx;
Integer needIdx;
Integer serviceIdx;
Integer phoneIdx;
Integer warrantyIdx;
Integer poIdx;
Integer descriptionIdx;
Integer initiatorIdx;
Integer orderIdx;
String locationFinal;
String needFinal;
String serviceFinal;
String tradeFinal;
String InboxFinal;
String InitiatorFinal;
String TimeFinal;
String poFinal;
String phoneFinal;
String servicenofinal;
String warrantyFinal;
String descriptionFinal;
String orderFinal;

    
global Messaging.InboundEmailResult handleInboundEmail
           (Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){  
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 if (email.fromaddress =='customerone@testl.com')
               {
                   
                   
location = 'Client: ';   
need = 'Need: '; 
service = 'Service Request #: '; 
phone = 'Client Contact Phone: ';

locationIdx = email.plainTextBody.indexOf(location);
needIdx = email.plainTextBody.indexOf(need);
serviceIdx = email.plainTextBody.indexOf(service);
phoneIdx = email.plainTextBody.indexOf(phone);
String[] bodySplitted = email.PlainTextBody.split('\n');
String descriptionFinal = email.plainTextBody.substringBetween('-----------------------------------------------------------------------------------' , '---------------------------------------------------------------------------------------------' ).normalizeSpace();                  
locationFinal = email.plainTextBody.substring(
                              locationIdx + location.length(), email.plainTextBody.indexOf('\n', locationIdx + location.length()));
String[] locNme = locationFinal.split(' ', 3);

needFinal = email.plainTextBody.substring(
                             needIdx + need.length(), email.plainTextBody.indexOf('\n', needIdx + need.length()));
serviceFinal = email.plainTextBody.substring(
                              serviceIdx + service.length(), email.plainTextBody.indexOf('\n', serviceIdx + serrvice.length()));
phoneFinal = email.plainTextBody.substring(
                              phoneIdx + phone.length(), email.plainTextBody.indexOf('\n',phoneIdx + phone.length()));
  
tradeFinal = email.plainTextBody.substringBetween('Not to Exceed: ', 'Service Level: ');
InboxFinal = email.plainTextBody.substringBetween('Email Delivery Time: ', 'Client: ');                   
InitiatorFinal = email.plainTextBody.substringBetween('RFS Initiator Name: ' , 'Client Contact Phone: ');           
TimeFinal = email.plainTextBody.substringBetween('Service Requested: ' , 'Email Delivery Time: ');    
poFinal = email.plainTextBody.substringBetween('Service Request #: ' , 'RFS State: ');                   
servicenoFinal = email.plainTextBody.substringBetween('Service Request #: ' , '(Include this # on Invoice)');              
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= 'BUNNSERVE REQUEST';
c.Case_Type__c= 'Service Request';
c.Priority='High';
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= descriptionFinal  + '\n' +  'Not to Exceed:  ' + tradeFinal + '\n' +  'Service Requested: ' + TimeFinal;
c.KA_PO__c= poFinal;
c.Location_Contact_Name__c = InitiatorFinal; 
c.BSP_Location_Contact_Phone__c = phoneFinal;
upsert c; 
}
catch(System.dmlException e)
     {System.debug('Error: Unable to create new Case: ' + e);
     }         
      
              }
        else if (locationFinal == null){
              System.debug('No Location was found');    
        }
      
    }
           
  else if (email.fromaddress =='customertwo@sample.com')
              
{
         
location = 'Site: ';   
Warranty = 'Warranty: '; 
phone = 'Caller Phone: ';
po = 'The referenced work order number is ';
description = 'Problem Description: ';
initiator = 'Caller name: ';
phone = 'Caller phone: ';
locationIdx = email.plainTextBody.indexOf(location);
WarrantyIdx = email.plainTextBody.indexOf(Warranty);
phoneIdx = email.plainTextBody.indexOf(phone);
poIdx = email.plainTextBody.indexOf(po);
descriptionIdx = email.plainTextBody.indexOf(description);
initiatorIdx = email.plainTextBody.indexOf(initiator);
phoneIdx = email.plainTextBody.indexOf(phone);
                   
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);

WarrantyFinal = email.plainTextBody.substring(
                              WarrantyIdx + Warranty.length(), email.plainTextBody.indexOf('\n', WarrantyIdx + Warranty.length()));
poFinal = email.plainTextBody.substring(
                              poIdx + po.length(), email.plainTextBody.indexOf('\n',poIdx + po.length()));
descriptionFinal = email.plainTextBody.substring(
                              descriptionIdx + description.length(), email.plainTextBody.indexOf('\n', descriptionIdx + description.length()));
initiatorFinal = email.plainTextBody.substring(
                              initiatorIdx + initiator.length(), email.plainTextBody.indexOf('\n', initiatorIdx + initiator.length()));
phoneFinal = email.plainTextBody.substring(
                              phoneIdx + phone.length(), email.plainTextBody.indexOf('\n',phoneIdx + phone.length()));  

orderFinal = email.plainTextBody.substringBetween('Date Entered: ', 'Region: ');
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= 'BUNNSERVE REQUEST';
c.Case_Type__c= 'Service Request';
c.Priority='High';
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= descriptionFinal + '\n' +'\n' + 'Warranty: ' + WarrantyFinal + '\n' + '\n' + 'Date Entered: ' + orderFinal;
c.KA_PO__c= poFinal;
c.Location_Contact_Name__c = InitiatorFinal; 
c.BSP_Location_Contact_Phone__c = phoneFinal;
upsert 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 ()
}

 
Krish NKrish N
Any help guys?