Cloud Pacific was tasked to develop a way to capture emails within Salesforce and create them as Email Message records that could be linked to their related Salesforce record.
Set-up and Configuration
1. In Salesforce, go to Setup, enter and select “Email Services” in the quick find box. Once redirected to the Email Services page, you will receive a notification about creating an Apex class before creating an Email Service.
2. Navigate to the developer console, select File, then New, and then Apex Class. Give the class a name.
3. Copy the code from the notification in the Email Services section and paste it into the Apex class that you created.
4. Navigate back to the Email Services section within Setup and click the New Email Service button.
5. In the Email Service Name field (1), add a unique name to identify the Email Service. In the Apex Class field (2), assign the Apex class to the Email Service by clicking the search icon and selecting the class that was created in the previous step. The Accept Attachments field (3) is used to determine what type of attachment will be received. In this case select “None”. If you need to define which email addresses to receive emails from, you can specify that within the Accept Email From field (4) or if you want to receive emails from any email address, leave the field blank. Finally, ensure the Active checkbox is enabled to activate the Email Service.
6. Once the Email Service configuration is complete, define the events for when an email fails. Here you will see the reasons (1) why an email can fail and the failure response options (2) that can be taken when it fails:
Over Email Rate Limit Action: Determines what the email service does with messages if the total number of messages processed by all email services combined has reached the daily limit for your organization. Salesforce calculates the limit by multiplying the number of user licenses by 1,000; a maximum of 1,000,000. For example, if you have 10 licenses, your org can process up to 10,000 email messages a day.
Deactivated Email Address Action: Determines what the email service does with messages received at an email address that is inactive.
Deactivated Email Service Action: Determines what the email service does with messages it receives when the email service itself is inactive.
Unauthenticated Sender Action: Determines what the email service does with messages that fail or do not support any of the authentication protocols if the Authenticate Senders checkbox is selected.
Unauthorized Sender Action: Determines what the email service does with messages received from senders who are not listed in the Accept From Email text box on either the email service or email service address.
The failure response options are:
Bounce Message: The email service returns the message to the sender or to the Automated Case User for On-Demand Email-to-Case, with a notification that explains why the message was rejected.
Discard Message: The email service deletes the message without notifying the sender.
Requeue Message (Over Email Rate Limit Action Only): The email service queues the message for processing in the next 24 hours. If the message is not processed within 24 hours, the email service returns the message to the sender with a notification that explains why the message was rejected.
7. For this example, the email will Bounce to a specific email address added in the Route Error Email to This Email Address
8. Once the Email Service and the response to failures have been configured, it’s now time to configure the email in which all emails will be received. Click on the New Email Address button to go to the email configuration page.
9. At the top of the configuration page you will see the Email Service Information section In the Email Address Name field (1) add a unique name to identify the email. The Email Address field (2) will populate with the email that will be used. The Context User field (3), defines the user in which the Apex class will be run and any other automation that is related to the emails. For example, the emails received with this Email Service will be assigned to this user as the owner. The Accept Emails From field (4) works in the same way as in the Email Service configuration by enabling email addresses that are placed in this field to send emails to the address in the Email Address field. If you want to receive it from any email address this field should be left blank.
10. Click Save. The Email Addresses list view shows the new Email address and the Context user.
11. Before sending the email to the email address generated by Salesforce, it is necessary to configure a way to save the new email address. In this example, an Email Message record will be created. Go to the Apex class and add the following piece of code to insert the record.
Note: In the relatedToId field, you are placing the Subject of the mail. In this example, the Subject will be used to place the Id of the record to which you want to place the record.
12. In Salesforce, we will create an account and after the record is created pick the record Id which can be found in the URL from the record.
13. Navigate to your Mail app and write an email where the recipient is the Salesforce-generated email, the Subject is the Account record ID, and the Body contains a test message. Click send.
14. In Salesforce, navigate to the recently created Account and locate the Email that was just sent.
15. Open the record of the Email Message. Ensure the Account record ID is the Subject and the Body and the record related to the email message are correct.
16. Below is an example of when an email fails to be sent. In this case, an error email was sent because the Email Address from which the email was sent does not have permission to send emails to the address generated by Salesforce. In other words, the email address was not added in the Accept Email From field in the Email Service configuration nor in the Accept Email From field in the Email Service Address configuration.
With Salesforce's email service, you can send and receive emails within the Salesforce platform, enabling you to conveniently track and monitor all your customer and prospect communications in one place. This eliminates the need to switch between multiple applications to manage your email communication and enables a streamlined workflow.
Furthermore, you can customize email services according to the functionality you require. For instance, in this blog, the email service produces Email Messages records, however, if an email service is needed for Leads that generate Tasks with every email, you can create a different email service. Both email services can coexist simultaneously, allowing you to segment your email lists and personalize your email content. The Salesforce email service offers you greater flexibility in managing your email communication, empowering you to tailor your approach to your specific business needs.