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
Daniel Johnson 8Daniel Johnson 8 

Looking for some help with email services.

Hi All,
I am attempting to create an email service that creates a lead and processes the information within the body of the email. I have been getting errors for a few days now so I figured its time to ask the community. Below is the code...Thanks in advance!!



global class PurchasedLeads implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
    String emailBody = email.plainTextBody;
    String companyName = emailbody.Substring(emailbody.IndexOf('Website Entered On:') + 20, emailbody.IndexOf('Practice Area:') - emailbody.IndexOf('Website Entered On:')-20);
    String firstName = emailbody.Substring(emailbody.IndexOf('First Name:') + 12, emailbody.IndexOf('Last Name:') - emailbody.IndexOf('First Name:')-12);
    String lastName = emailbody.Substring(emailbody.IndexOf('Last Name:') + 11, emailbody.IndexOf('Email:') - emailbody.IndexOf('Last Name:')-11);
    String emailAddress = emailbody.Substring(emailbody.IndexOf('Email:') + 7, emailbody.IndexOf('Phone Number:') - emailbody.IndexOf('Email:') - 7);
    String phoneNumber = emailbody.Substring(emailbody.IndexOf('Phone Number:') + 14, emailbody.IndexOf('Request Details') - emailbody.IndexOf('Phone Number:') - 14);

    Lead[] newLead = new Lead[0];
     try {
       Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
       Schema.SObjectType s = sObjectMap.get('Lead') ;
       Schema.DescribeSObjectResult resSchema = s.getDescribe() ;

     newLead.add(new Lead(Company = companyName, LastName = lastName, Email = emailAddress,Phone = phoneNumber));
       insert newLead; 
            
    }
    
   catch (QueryException e) {
       
   }
   
   result.success = true;
   return result;
  }
}
Pankaj_GanwaniPankaj_Ganwani
Hi Daniel,

Can you please specify what errors you are getting? Are you getting the errors related to the email service is not being invoked when you send an email?
Pankaj_GanwaniPankaj_Ganwani
Hi Daniel,

One more thing, describe call is not needed here since you are directly populating the field values in Lead object.
Daniel Johnson 8Daniel Johnson 8
The error I am receiving is: (Undelivered): 554 The apex class PurchasedLeads failed due to: System.StringException: Ending position out of bounds: 69
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please make sure that you are sending email to the address specified in email service(which is long one) and your email is added in email service's Accept from email?
Daniel Johnson 8Daniel Johnson 8
It is set to allow from all incoming email addresses and I could receive emails before I added the code to create a new lead.
YuchenYuchen
One possible reason of this "System.StringException: Ending position out of bounds" is your substring part, I saw you have several emailbody.Substring, you may want to make sure that the ending position of the string is within the length of the string. For example, if the length of the string is 10, but you are trying to access the 15th index, then there will be some error.
Daniel Johnson 8Daniel Johnson 8
Here is the debug log:


33.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 14:53:43.033 (33941891)|EXECUTION_STARTED 14:53:43.033 (33975922)|CODE_UNIT_STARTED|[EXTERNAL]|01pj0000003bgdT|PurchasedLeads.handleInboundEmail 14:53:43.037 (37137833)|METHOD_ENTRY|[1]|01pj0000003bgdT|PurchasedLeads.PurchasedLeads() 14:53:43.037 (37147597)|METHOD_EXIT|[1]|PurchasedLeads 14:53:43.037 (37717802)|SYSTEM_CONSTRUCTOR_ENTRY|[6]|<init>() 14:53:43.037 (37752285)|SYSTEM_CONSTRUCTOR_EXIT|[6]|<init>() 14:53:43.037 (37788043)|SYSTEM_METHOD_ENTRY|[9]|String.indexOf(String) 14:53:43.037 (37815481)|SYSTEM_METHOD_EXIT|[9]|String.indexOf(String) 14:53:43.037 (37831347)|SYSTEM_METHOD_ENTRY|[9]|String.indexOf(String) 14:53:43.037 (37840756)|SYSTEM_METHOD_EXIT|[9]|String.indexOf(String) 14:53:43.037 (37849861)|SYSTEM_METHOD_ENTRY|[9]|String.indexOf(String) 14:53:43.037 (37858131)|SYSTEM_METHOD_EXIT|[9]|String.indexOf(String) 14:53:43.037 (37872161)|SYSTEM_METHOD_ENTRY|[9]|String.substring(Integer, Integer) 14:53:43.037 (37945185)|SYSTEM_METHOD_EXIT|[9]|String.substring(Integer, Integer) 14:53:43.037 (37984327)|FATAL_ERROR|System.StringException: Ending position out of bounds: 69 Class.PurchasedLeads.handleInboundEmail: line 9, column 1 14:53:43.037 (37995915)|FATAL_ERROR|System.StringException: Ending position out of bounds: 69 Class.PurchasedLeads.handleInboundEmail: line 9, column 1 14:53:43.142 (38896860)|CUMULATIVE_LIMIT_USAGE 14:53:43.142|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 100 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000 Maximum heap size: 0 out of 36000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10 14:53:43.142|CUMULATIVE_LIMIT_USAGE_END 14:53:43.038 (38922487)|CODE_UNIT_FINISHED|PurchasedLeads.handleInboundEmail 14:53:43.041 (41653980)|EXECUTION_FINISHED
 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please put the debug statement at line no 69 in your code and check whether the string size is more than what you are trying the access?
Daniel Johnson 8Daniel Johnson 8
Thanks for the help guys. I was able to get this going by changing the code. Beneath the screenshot you will find the new apex class I am using for the service. I am however having one more issue. When the information is inserted it is adding extra characters before and after each word and phone number. I am assuming that the phone number is because the email has html elements? Not sure

User-added image
any suggesstion?   

New Code:

global class PurchasedLeads implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
    
    String[] emailBody = email.plainTextBody.split('\n', 0);
    String companyName = emailBody[8].substring(20);
    String firstName = emailBody[16].substring(12);
    String lastName = emailBody[17].substring(11);
    String phoneNumber = emailBody[19].substring(14);
    
    Lead[] newLead = new Lead[0];
     try {
       newLead.add(new Lead(Company = companyName,FirstName =  firstName,LastName = lastName,Phone = phoneNumber));
       insert newLead;        
    }
    
   catch (QueryException e) {
       
   }
   
   result.success = true;
   return result;
  }
}
YuchenYuchen
Hi Daniel,

Looks like you get the phone number from "String phoneNumber = emailBody[19].substring(14);", could you print the emailBody and emailBody[19] in the debug log and see what it returns. If it always has "<" or space between the phone number and the other html elements, maybe you can split the emailBody[19] part again, then the first array returned is the phone number.