2

I am currently looking to implement some email to Lead functionality into an org, I am going to look to use emailService which hooks onto an inbound email class. Similar to this help article.

A third party site will generate the emails and send them into salesforce. Once the email comes in I need a smart way of traversing the body of the which would look something like:

New user registered

Name: Tom James

Position: Owner

And mapping the inbound information to fields in salesforce. Just looking for direction on how to apply something like this.

3
  • 1
    You have just linked to the documentation with examples yourself... Commented Apr 11, 2017 at 14:15
  • Theres no mention of pulling information out of the body of the email and mapping it though
    – Nebbyyy
    Commented Apr 11, 2017 at 14:18
  • 1
    But there is a very similar situation in Example 2: Handle Unsubscribe Email Commented Apr 11, 2017 at 14:20

1 Answer 1

3

you have to define a new email service like below : -

  1. create a Apex class named like "CreateRecordFrmEmail"

  2. Your Name | Setup | Develop | Email Services .

  3. refer the class which will be given below .

and use this class based on you logic.

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

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

        String emailbody = email.body;//you can get email body
        String emailsubject = email.subject;//you can get email Subject
        String subToCompare = 'New user registered';

        //put you logic for your three parameters like Name, Position and subject.
        if(email.body.equalsIgnoreCase(subToCompare))
        {
            //Do your record creation or other logic.

           // Using this Save attachments, if any
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
            Attachment attachment = new Attachment();

                attachment.Name = tAttachment.fileName;
                attachment.Body = Blob.valueOf(tAttachment.body);
                attachment.ParentId = c.Id;
                insert attachment;
            }

            //You can Binary Attachment also
            for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
                Attachment attachment = new Attachment();

                attachment.Name = bAttachment.fileName;
                attachment.Body = bAttachment.body;
                attachment.ParentId = c.Id;
                insert attachment;
            }
        }

    result.success = true;
        return result;
    }
}

it will solve your purpose.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .