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
intern2424intern2424 

Help with inbound Email message and apex

Hi I am trying to create an object from an inbound email message. This is the class I created:

 

global class Person 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 name = emailBody[0].substring(0);
String age = emailBody[1].substring(0);
String birthdate = emailBody[2].substring(0);
String city = emailBody[3].substring(0);
String street = emailBody[4].substring(0);
String zip = emailBody[5].substring(0);

Person__c[] newPerson = new Person__c[0];

newPerson.add(new Person__c(
               Name = name,
               Age__c = age,
               Birth_Date__c = birthdate,
               City__c = city,
               Street__c = street,
               Zip_Postal_Code__c = zip));

insert newPerson;

return result;
}

But when I send an email I keep on getting this error:

 

554 System.ListException: List index out of bounds: 1

Class.Person.handleInboundEmail: line 13, column 24

External entry point

 

 

Thank you for any help you can give. 

 

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

My guess is your String[] emailBody is not being set properly with the split() method. I recommend using System.debug messages to output the plainTextBody and the emailBody string array to ensure its as you expect.

 

If you are trying to split() based off the newline character, isn't that '\n' and not '/n'?

All Answers

aalbertaalbert

My guess is your String[] emailBody is not being set properly with the split() method. I recommend using System.debug messages to output the plainTextBody and the emailBody string array to ensure its as you expect.

 

If you are trying to split() based off the newline character, isn't that '\n' and not '/n'?

This was selected as the best answer
intern2424intern2424

Yeah, I just figured that out a minute ago. Man. HAHAHA. I just got it to work this way. I was wondering if I could build the SOQL statement dynamically. 

 

My email will look like this: (First line is the object I created, second line is the name i want to put in, third line is the field name, and so on)

 

Person__c

John Doe

Name

27

Age__c

04/04/1988

Birth_date__c 

Dallas

City__c

Saint Tom

Street__c 

12345

Zip__c 

 

 

Could i create a for loop to build the statement and just do:

 

newPerson.add(new "This is were i put my new SOQL statement"));

 

 

Thank you for all the help.