Apex

Inbound Email Handler in Salesforce


We all write Workflows or triggers to send email from Salesforce but did you know that you can send emails back to Salesforce org and perform task based on the content of the email ?


Inbound Email Service :

Email Service is an apex class which implements Messaging.InboundEmailhandler interface.  This interface enables you to read the Subject,Content,headers and attachment of the incoming email. You read the email in the Email service class and perform DML operations such as creating new records or updated the existing ones as per your requirement. 

To create Email services click Setup--> Develop--> Email Services and then create a apex class using the sample code mentioned on the email services landing page. See the snapshot below : 

 Once the class is create go ahead and click on New Email Service button enter the information as displayed in the image below, Change the Error routing address from info@worldofsalesforce.com to your email address so that you receive error notifications when the email processing fails. 

Click on "Save and New email Address" , Once done choose the context user and keep the Accept email from field blank to accept emails from all kind of domains. Clicking on Save will generate a email address automatically ,this email address will look somewhat like


 Now we can write the logic which is to be executes when an email is sent to the above created email address. Copy the below code and replace it in the email service class created initially in the post. (myHandler )


global class myHandler  implements Messaging.InboundEmailHandler {

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

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

    Contact con = new Contact();
    con.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
    con.LastName = email.fromname.substring(email.fromname.indexOf(' '));
    con.Email = envelope.fromAddress;
    insert con;

    return result;

  }

}

Voila !! Our email handler is ready which creates a contact record whenever an email is sent to the Salesforce auto-generated email address.  

Feel free to ask question or comment !

About Saurabh Dua

4 comments:

  1. Hi Saurabh,

    Thanks for sharing this Code. My issue is I am having a large email Body and Need to extract the information from the email body. And I want to create the Lead (not Contact). I tried but not able to succeed. Please help me to extact the right information from the email body in order to create the Lead. Looks like it is an complex situation and The email address and Subject line are not correct for our organization as it is sent my the third party.

    Appreciate your advice!

    ReplyDelete
    Replies
    1. Kindly post the email body and explain the details of what needs to be extracted. Ill post the code which is required to achieve the desired result .

      Delete
  2. while am searching for lookup "myHandle" unable to find it

    ReplyDelete
  3. Variable does not exist: FirstName ?? Any one Can help me??

    ReplyDelete

Note: only a member of this blog may post a comment.

Powered by Blogger.