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

Sending an email and building a object.

Hi, I have a question about sending a email and then building an object. I created a method that parses an email out and sends that data in an email my salesforce account. I was wonder how on the salesforce end do you parse that data and add it to an object that is already been created. 


Thanks for any help you can give. 

Always ThinkinAlways Thinkin


You need to perform a SOQL query against the target object's table to generate a result set of records to which you want to attach (or update) the new data. It easiest if you can use an attribute of the email's envelope such as the sender's email like this "SELECT id FROM WHERE = email.FromAddress". Once you have the record id, you can use the data in the email to either update information in the original record or add a related record such as a Task with the email's data.


Here's a sample where I do both:


global class LeadRepliesService implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ // Create an inboundEmailResult object for returning the result of the Apex Email Service Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String myPlainText= ''; // Add the email plain text into the local variable myPlainText = email.plainTextBody; // New Task object to be created Task[] newTask = new Task[0]; // Try to lookup any Leads based on the email from address // If there is more than 1 lead with the same email address, // the first one matched will be assigned the email. // AN order by CAN TARGET THE ACTIVE LEAD IF THERE IS A CONSISTENT RULE FOR PREDICTION Lead vLead = [Select Id, OwnerId From Lead l Where Email = :email.fromAddress AND IsConverted = false Limit 1]; //Check for email opt out phrase in Subject and if found, set Lead to Dead and Email Opt Out if (email.Subject == 'I No Longer Want to Receive Information About your company'){ vLead.Lead_Temperature__c = 'Dead'; vLead.HasOptedOutOfEmail = true; update vLead; } else { // Add a new Task to the lead record we just found above. newTask.add(new Task( OwnerId = vLead.OwnerId, Subject = 'New Email: ' + email.subject, Description = myPlainText, Priority = 'High', Status = 'Inbound Email', IsReminderSet = true, ReminderDateTime =, WhoId = vLead.Id)); // Insert the new Task insert newTask;


 (caveat: I clipped this from a larger script, might be missing a bracket or variable declaration or something)

Hope that helps,

Luke C



Hi, I can up with something like this but I still get an error:


global class Person implements Messaging.InboundEmailHandler
global Messaging.InboundEmailResult
handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env)
Messaging.InboundEmailResult result = new

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;



This is the error i keep on getting:


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

Class.Person.handleInboundEmail: line 13, column 24
External entry point

Joe Thumb


saint anthony




Thank you for any help you can give.