Inbound Email Handler in Salesforce | Techila
1204
single,single-post,postid-1204,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Inbound Email Handler in Salesforce

blog-21

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 : 

Description: http://www.techilaservices.com/techilla-oldsite/wp-content/uploads/2016/07/InboundEmailHandlerInSalesforce2.png 

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. 

 

Description: http://www.techilaservices.com/techilla-oldsite/wp-content/uploads/2016/07/InboundEmailHandlerInSalesforce3.png

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

testemailservice@2svpc6svbc5h502gki5uodsmfacy48ukb4voyk8183er5j6jdd.28-1jv8weaw.ap2.apex.salesforce.com

 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;

  }

}

 

Courtesy http://www.worldofsalesforce.com/2016/05/InboundEmailHandlerSalesforce.html

Author: techila

No Comments

Post A Comment