1

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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.