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
Spencer WidmanSpencer Widman 

APEX class to Parse email body to update fields on multiple contact records or Parse data in an attachment

I get sent emails with data in both the bodies and as a csv attachment with contact information that I need to update.  Currently I use the dataloader or the data import wizard and manually upload the information into our Org.  From the research I have done on the forums it appears that I can write an Apex class to parse the information from both the body of the email or an attachment and update the contact records with the appropriate information.  Looking for some examples or resources to write this in Apex so that my external partners can email an address and have the data update in my organzation.  Hopefully this makes sense on this post like it does in my head.  Any information or references would be greatly appreciated.  
Greg HGreg H
Use an Inbound email handler: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_inbound_using.htm.

This may help to get the ideas flowing:
public class emailHandlerClass implements Messaging.InboundEmailHandler {
    
    //access an InboundEmail object to retrieve the contents, headers and attachments of inbound emails
    public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,  Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); //InboundEmailResult object for returning the result of the Apex Email Service 
        String emailBodyAsString = email.plainTextBody; //email body
        if (email.BinaryAttachments != null) { //if there were binary files attached to the email
            for (Messaging.InboundEmail.BinaryAttachment b : email.BinaryAttachments) { //loop over all the files 
                //do something with them...
            }
        } else if (email.textAttachments != null) { //otherwise, if there were text files attached to the email
            for (Messaging.InboundEmail.TextAttachment t : email.textAttachments) { //loop over the text files
                //do something with them...
            }
        }
        //do something using the email body
    }

}

Good luck.
-greg
Spencer WidmanSpencer Widman
Greg, Thanks so much for the starting point and the reply. I’ll post back my progress once I start getting some things figured out. Thanks again for the help! Spence
Spencer WidmanSpencer Widman
Thanks for the info so far.  It has helped guide my research.  Part of the problem is my limited experience with APEX.  I thought I would expand some on my use case in an ttempt to get a more narrowed starting point and code examples.  The emails I receive are lists of contacts with field info that need updated.  The information comes as either a .csv attachment, and .xlsx attachemnt or in the form of a forwarded salesforce report with the info embedded in the email.  Below is a sample of info embedded in the body of the email:

First     Name    Last Name    Email    Created Date
Sean    Sample    sampleyahoo.com    8/1/2019
Stacey    Sample  sample2@outlook.com    7/31/2019
Rick    Sample  sample@hotmail.com    7/30/2019
Brian    Sample  sample@gmail.com    7/30/2019
Daniel    Sample      sample@gmail.com   7/30/2019
Asiel "Ace"    Sample      sample@gmail.com    7/29/2019

When the data is received as one of the two attachments the data is in standard spreadsheet form with header rows very similiar to the embedded.  In all cases the contacts exist and just need to be updated.  with the appropriate information in the various fields.  

Does anyone have any ideas for resources that I could use specific to this use case or some sample code I might use to get going?  The code Greg H posted looks to be exactly inline with what I need but due to my lack of experience I need some examples or resources to write the execution data.  Thanks in advance for any help you all might have.  

Cheers!

Spence
Greg HGreg H
This may be a good starting point for parsing a CSV file: https://gist.github.com/nicocrm/858086
-greg