Implementing Complex Business Logic Using Apex Email Service

You are currently viewing Implementing Complex Business Logic Using Apex Email Service

Simply put, I love Apex Email Service. It gives you the power of processing an inbound Email , extract the needed information and then dump it into Salesforce the way you want. The contents, headers and attachments of the Email are all at your disposal which you could play with and then exploit to implement complex business logic. I would highly recommend checking out the official developer docs if you are new to Email Services. Want to process the attachment on an incoming email and update a record or want to create a case from a voicemail (sent as a transcribed Email) or want to update records in Salesforce using test results from an external automation tool? Then welcome to the world of Apex Email Service. Here are the two use cases that I will be covering in this post:

  1. Process a transcribed Voicemail forwarded from an application like Microsoft Exchange Server with Voice Mail Preview and manipulate data in Salesforce
  2. Process an Email sent from a test automation tool and manipulate data in Salesforce

The logic used for both the requirements will be almost similar in terms of parsing data out of the email, doing a lookup and then creating or updating records in Salesforce. The only difference will lie in what is parsed out of the email and how the email content is used to perform the DML.

  1. Process a Transcribed Voicemail and create a Case – Implementing your own Voicemail To Case

    If your company uses something like a Microsoft Exchange Server with the ability to send transcribed voicemails as an email, Apex Email Service is a good candidate for processing this email and creating a case out of it in Salesforce. The business logic for setting the Case Owner, Contact and Account on the newly created Case will differ from organization to organization so I will focus on laying out the fundamental parts of such an implementation.

    Source of Information: Transcribed Voicemail containing the Phone Number
    Lookup Logic: Parse out the Phone Number, locate the Contact with this Phone Number and retrieve associated Account. Also determine the Queue based on any Account attribute
    Final Action: Create Case and set the Contact, Account and Case Owner

    Parsing the Phone Number


    Processing Attachments
    Processing Attachments


    Create the Case


    Main Method for Logic
    Main Method for Logic

    The above posted code is just a sample so tweak it your heart’s content to suit your needs.

  2. Process a Test Automation Tool Email and update an Account – Implementing Integration with an External tool without making API calls

    Requirement: A company run Disaster Recovery tests on an application that they distribute to their clients on a hosted server. The test automation tool that they use to run the burn scripts does not provide an integration with Salesforce (no API access). It does however send a test results email containing the client ID’s and the burn test results in the form of a Pass/Fail. We need to get this Email into Salesforce and update the relevant custom fields on the clients/accounts records to be able to report on the disaster recovery burn test results.

    Source of Information: The test results Email generated from an automation tool like HP QTP or Telerik. For simplicity, we will assume that this Email only contains failure results.
    Lookup Logic: Parse out the Account ID’s, look up the accounts in Salesforce
    Final Action: Update two custom fields with Pass/Fail result and a date/time stamp of the test run

    The image below shows a preview of what the automated test results email looks like. The idea is to scan the email body for account Id’s that failed the test and then execute the required logic. I used Telerik to perform the automated testing and almost every popular test automation tool can be configured to send an email like the one below:

    Sample Test Results Email Body
    Sample Test Results Email Body


    Test Tool Email Integration
    Test Tool Email Integration

As you must have noticed,   it’s tremendously easy and convenient to utilize Emails from any possible source and process them to manipulate Salesforce data as you like. The above use cases themselves can be further enhanced and customized to support more complex logic so play around with it and open up a whole world of possibilities with Apex Email Services.

Have you used Email Services to accomplish a requirement in your Salesforce instance? Feel free to share in a comment below!