Email Services Apex Class in Salesforce: Contact Handling and Extraction

A primer on the Process

For the uninitiated, the Email Service in Salesforce is an automated Process that strips content bodies, subtext, headers, and attachments from inbound emails for Object creation.

 

You could go about tieing each Email Service Process to Salesforce-created email addresses meant to receive messages for processing from users of a service.

 

Alternatively though, to give multiple users access to a service you could associate a single Salesforce email to an Email Service and write a class in Apex to execute differently for each user.

A common use case

As an example, you could write Apex Classes to identify users based-off of their email addresses and create Records accordingly.

Let’s talk Governor Limits

As is often the case with email capabilities, there’re Governor Limits to keep in mind. Here’s a list of them:

 

  • An email service only processes messages it receives at one of its addresses.
 
  • Salesforce limits the total number of messages for all email services combined for daily processing, including On-Demand Email-to-Case. Messages exceeding this limit are bounced, discarded, or queued for processing the next day, depending on how you configure the failure response settings for each Email Service.

Salesforce calculates the limit by multiplying the number of user licenses by 1,000 and caps out at 1,000,000. For example, if you have 10 licenses, your Org can process up to 10,000 email messages a day.

 

 

  • Email Service addresses created in your sandbox can’t be copied to your production Org.
 
  • For each Email Service, you can tell Salesforce to send error email messages to a specified address instead of the sender’s email address.
 
  • Email Services reject email messages and notify the sender of the email(comprised of combined body text, body HTML and attachments that extend approximately 10 MB depending on language and character set).

Now that you’re a bit more familiar, here is some syntax for you to look at. First of these – is the Email Service.

General syntax for Email Service class in Salesforce

Some more syntax

For our example, we start with the premise that each email’s subject and body contain no more than the words “Create Contact” and the Contact’s name(in that order).

 

Then, in the likeness of an everyday enterprise scenario, we process subject lines to create new Contacts in Salesforce and extract Contact Names from the email body. Attachments come up under the Contact object in sales.

 

In the following sections, you’ll find code implementations and test classes( 75% coverage) for the same.

Apex Class:-

Test Class:-

Defining Email Services with our Apex Class

 

After creating the above Apex Class, within your instance, go to

 

<Your Name> | Setup | Develop | Email Services.

Once there,

 

  • Click New Email Service to define a new Email Service.

Select the Apex Class we just created above, add the email address from where to accept the request and activate the Service as shown below:

After filling out the information, click Save and New Email Address

Next, click Save. This should throw up an email address as shown below.

Note: The domain name in the Email Address must qualify the one entered at Email Service in the first step. For instance, we’ve entered “gmail.com” in the first step, which means it will accept the email address only from gmail.com like the one in the email we just saw.

 

Finally, once the email address is generated by Salesforce, send the email to that address with the subject line “Create Contact” and a Contact Name as the email body to see if everything checks out.

Conclusion

The codebase here lets you extract data from emails for creating Contacts in your Salesforce Org that could be used to service Contacts from a business account in case of multiple users, especially for helpdesk operations or pushing updates and notifications. You could just as well extend this to build on stores for other objects. For any further assistance with Email feature sets or data extraction solutions for the Salesforce platform, don’t wait to reach out to us at sales@hicglobalsolutions.com

Our Location worldwide
India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
SR Tower 2nd Floor Hydel Gate Haldwani Uttarakhand 263126 +91-5946359996
USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818