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
GRStevenBrookesGRStevenBrookes 

Problem with Email Service

Hi,

 

I am trying to create a record on Lead from an inbound email however am receiving error:

 

The attached message was sent to the Email Service address <inbound_email_to_lead@p-5mwzfrm0nxt6p749ki530zlzk.2ntneeac.2.apex.salesforce.com> but could not be processed because the following error occurred:

554 System.StringException: Starting position out of bounds: 10

Class.CreateLeadfromEmail.handleInboundEmail: line 40, column 1

 Has anyone any ideas - here is the class:

 

/**
 * Email services are automated processes that use Apex classes
 * to process the contents, headers, and attachments of inbound
 * emails.
 */ 
    
global class CreateLeadfromEmail implements 
        Messaging.InboundEmailHandler {

  // Creates new candidate and job application objects  
    
 
Lead[] newLead = new Lead[0];

 global Messaging.InboundEmailResult handleInboundEmail(
  Messaging.InboundEmail email, 
  Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = 
        new Messaging.InboundEmailresult();
 
  // Captures the sender's email address  
    
String emailAddress = envelope.fromAddress;
 
  // Retrieves the sender's first and last names  
    
String fName = email.fromname.substring(
        0,email.fromname.indexOf(' '));
String lName = email.fromname.substring(
        email.fromname.indexOf(' '));
 
  // Retrieves content from the email.  
    
  // Splits each line by the terminating newline character  
    
  // and looks for the position of the phone number and city  
    

String[] emailBody = email.plainTextBody.split('\n', 0);
String phoneNumber = emailBody[0].substring(10);
String mobileNumber = emailBody[0].substring(11);
String messagenotes = emailBody[0].substring(13);

 
  // Creates a new candidate from the information  
    
  // retrieved from the inbound email  
    
   try
    {
      newLead.add(new Lead(Email = emailAddress,
      FirstName = fName,
      LastName = lName,
      Phone = phoneNumber,
      MobilePhone = mobileNumber,
      Description = messagenotes
      ));
 
      insert newLead;
   }
    
   catch (System.DmlException e)
   {
System.debug('ERROR: Not able to create lead: ' + e);
   }
 
 
return result;
    }   
}

 any help most apprecaited,

 

Steve

sfdcfoxsfdcfox

That would happen if emailBody[0].substring(10) is equal to or greater than emailBody[0].length(), which it probably is in your test.

GRStevenBrookesGRStevenBrookes

what as in the email body is less than 10 lines? If so...no it isnt.

sfdcfoxsfdcfox

Your code is looking only at the first line of the email, and grabbing the 10th character on to the end of the line. So if your email looks like this:

 

New Lead
Lead Email: asdf@asdf.com
Lead Name: Jane Doe

Then emailBody[0].length() will be only 8, and your code would fail. You should probably loop through each line, make sure that the line starts with a given piece of text, and set the variable associated with that value, if any.