Email to Flow in Salesforce

Munawirrahman
5 min readFeb 7, 2021

--

Hi Folks!

These days, Email is still the most popular communication tool to interact with each other. And the good news is Salesforce understands about that, and they come up with so many solutions related to email automations like Email Alert, Email Services, Gmail Integration with Salesforce, Gmail Lightning Sync, etc.

All those email automations I mentioned above has different functionality. The one that I want to highlight here is Email Services.

Referred from Salesforce Help site. Email Services are automated processes that use Apex classes to process inbound email. Yes…. Apex class, you’ll need an Apex Class to handle Inbound email and do automation in your Salesforce Org.

But what if…. you can use Flow Builder to handle Email Inbound in your Salesforce org? hmm tempting isn’t it? That’s why in this article, I’d like to show you my generic solution on how I handled Email Inbound with AutoLaunched Flow.

Main functions of my solution are to:

  1. Route Email from your Email Services to Autolaunched flow (you can route this to any object you want to)
  2. Pass all variables from inbound email (or email service) to Flow variables
  3. Convert email attachments to ContentVersion records variable (if your email attachments contain text attachments, it will be converted to pdf in ContentVersion)
  4. AutoReply to email sender with a HTML text(optional)
Image00 How EmailToFlow works?

Attributes

Input from Email Service to flow

  1. Envelope_FromAddress
    type: Text / String
    description: The name that appears in the From field of the envelope, if any.
  2. Envelope_ToAddress
    type: Text / String
    description: The name that appears in the To field of the envelope, if any.
  3. Email_ToAddresses
    type: Text / String (Collection)
    description: The email address that appears in the To field.
  4. Email_FromName
    type: Text / String
    description: The name that appears in the From field, if any.
  5. Email_FromAddress
    type: Text / String
    description: The email address that appears in the From field.
  6. Email_CCAddresses
    type: Text / String (Collection)
    description: A list of carbon copy (CC) addresses, if any.
  7. Email_Subject
    type: Text / String
    description: The subject line of the email, if any.
  8. Email_ReplyTo
    type: Text / String
    description: The email address that appears in the reply-to header.
  9. Email_MessageId
    type: Text / String
    description: The Message-ID — the incoming email’s unique identifier.
  10. Email_References
    type: Text / String (Collection)
    description: The References field of the incoming email. Identifies an email thread. Contains a list of the parent emails’ References and message IDs, and possibly the In-Reply-To fields.
  11. Email_HTMLBody
    type: Text / String
    description: The HTML version of the email, if specified by the sender.
  12. Email_IsHTMLTruncated
    type: Boolean
    description: Indicates whether the HTML body text is truncated (true) or not (false)
  13. Email_TextBody
    type: Text / String
    description: The plain text version of the email, if specified by the sender.
  14. Email_IsTextBodyTruncated
    type: Boolean
    description: Indicates whether the plain body text is truncated (true) or not (false)
  15. Email_ContentVersion
    type: ContentVersion Records (Collection)
    description: Collection of converted email attachments (if attachment is text will be converted to pdf)

Output from Flow

  1. AutoReplyMessage (optional)
    type: Text / String
    description: define whether the Inbound email needs to be auto replied or not. If null, won’t send autoreply. Support HTML Message

How to setup

Step 1. Install EmailToFlow Package from here

<v.1.1>(support html in autoreply email)
Prod: https://login.salesforce.com/packaging/installPackage.apexp?p0=04tJ1000000olto
Sandbox: https://test.salesforce.com/packaging/installPackage.apexp?p0=04tJ1000000olto

<v.1.0 Deprecated>
Prod: https://login.salesforce.com/packaging/installPackage.apexp?p0=04t2w000009F5xV
Sandbox: https://test.salesforce.com/packaging/installPackage.apexp?p0=04t2w000009F5xV

This package includes
1. EmailToFlow class : Router from email service to flow
2. EmailToFlowTest class: Test class for the apex class
3. EmailToFlowController Autolaunched flow: Main flow to control your inbound email

Source code: https://github.com/munawirrahman/EmailToFlow

Step 2. Create a new email address to receive inbound email

Image01 Go to setup -> search Email Services
Image02 Config new Email Service. Fill apex class to EmailToFlow class. Config all necessary fields based on your need. And click save
Image03 Click New Email Address to generate salesforce email
Image04 configure email address information based on your need. And click save
Image05 You’ll get a new autogenerated email address that you can use to route email to your flow. This email will be the email address that you’ll share to client when you want to use this

Step 3. Test your email

Image06 Open EmailToFlowController flow. You’ll see there are prepackaged variables in your flow and there’s router to test your new autogenerated email
Image07 If you send an email to the autogenerated email address with subject Test Email To Flow, the router will continue to the next path
Image08 If there’s attachments in the email, it will create a new file
Image09 set the AutoReplyMessage from Text Template
Image10 The sender of the email, will automatically receive a reply with body from the email template
Image11 Testing send email with Test Email To Flow subject
Image12 Autoreply Message from the email service (In the same email thread)

There you go! once you set this up, you can do pretty much anything with this Email to Flow, like Create, Delete, Update records, Logging emailmessage to a record, autoforward the email with additional information from Salesforce, etc. But also remember that, you should have some kind of email inbound contract when using this, in my setup example, the contract is “The email subject should equal to Test Email To Flow” you can create your own contract based on your business requirements.

Thank you for reading, clap if you think this is useful, and comment if you have trouble when utilizing this! I’ll try to help the best as I can

--

--