Tech blog to share information.

May 17, 2018

Salesforce : Use Single Inbound Email Service to Process Various Emails .

May 17, 2018 Posted by tjcool , , No comments

- Use Single Inbound Email Service to Process Various Emails -

You can use Apex to receive and process email and attachments. The email is received by the Apex email service and processed by Apex classes that utilize the InboundEmail object.

For example - You can create an email service that automatically creates contact records based on contact information in messages

In this post, we will be covering, use case where Salesforce will be receiving an email from different automated systems say Fax system or from Particular email alias.

Part I - Apex Code for Dynamically Processing Email -

We will be invoking class dynamically in this part. Below are the steps for same.

a) Create Interface 

global class IEmailProcessor {
    object Process(Messaging.inboundEmail email);

b) Implement Class IEmailProcessor 

global class FaxProcessor implements IEmailProcessor  {
  public object Process(Messaging.inboundEmail email){
 //All email processing logic will come over here like record creation

Part II- Creating Apex Class for Email Service - 

1. Create Apex class and implement interface Messaging.InboundEmailHandler
2. Using "handleInboundEmail" method in that class, you can access an InboundEmail object to retrieve the contents, headers, and attachments of inbound email messages. "handleInboundEmail" has two parameters
   a) Inbound email: Represents an inbound email object and can say contains complete details about an email like attachments, message, sender details.
   b) InboundEnvelope: The InboundEnvelope object stores the envelope information associated with the inbound email, and has fields fromAddress and toAddress.

Snippet - 

global class EmailExample implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
    // Create an InboundEmailResult object for returning the result of the  Apex Email Service
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
       //Add the email plain text into the local variable 
       string sender=email.fromname;
       EmailProcessor__c apexClass= EmailProcessor__c.getValues(sender);
          //Create type
          Type type = Type.forName(apexClass.Apex_Class__c);

          //using new instance we create ew instance of class.  
          object instance = type.newInstance();
          IEmailProcessor interfaceInstance = (IEmailProcessor)instance;
          //invoke method
          object result= interfaceInstance.Process(email);
         result.success = false;
   // If an exception occurs when the query accesses 
   catch (QueryException e) {
       System.debug('Query Issue: ' + e);
   // Set the result to true. No need to send an email back to the user with an error message 
    result.success = true;
   // Return the result for the Apex Email Service
   return result;

Part III - Setting Up Email Service in Salesforce - 

Step1 -  Click Your Name > Setup > Develop > Email Services

Step2 & Step3 -

In my case apex, the class value would be: EmailExample 

Part IV - Create Custom Setting for Email and Apex class mapping. 

Below is a snapshot of my custom setting and field.

Populate custom settings with apex class and alias email address. Now when Salesforce will receive an email your class will get invoked and checks if the email is coming from configured service or not and if it so the appropriate class will get invoked.

Hope this Helpfull. Please Comment and Subscribe