CRM Authors: Xenia von Wedel, Ian Khan, PR.com Newswire, Steve Mordue

Related Topics: CRM

CRM: Case Study

Use Email Services for Customized Email-To-Case

With Code

Force.com Email Services is a great tool.  Sometimes called Email-to-Apex, it allows for the ability to create an email address within Salesforce that can kick off and process Apex code.  Setting up the email service address is quite simple; an overview can be found here on how to set it up.  There are a number of security and functionality options that can be handled declaratively, for example:

  • Whitelisting of email addresses (or domains) that the email address accepts from.
  • Handling of attachments
  • Failure/Error handling
  • Selection of a context user that's associated with the email address and Apex Class

I want to share one particular use case where the email service was really handy; it's a customized Email-to-Case situation.  While the standard email-to-case functionality within Salesforce makes it easy to create cases from incoming emails, all point-and-click which is great, it obviously lacks some abilities.

This example is a bit unique in that the organization's parent company captured information from a form on their website and forwarded it via an email.  There are only two types of emails being sent to this address, so the subject lines and the exact format of the text emails were known in advance. Let's say the emails look like this...

Hi, you have received this email from your parent company.
The following person has created a case.

Name: Joe Test
Role: Owner
Email: joetest@joetest.com
Phone: 555-555-5555
City: Anytown
Region: Georgia
Country: USA

What seems to be the problem? "This is where Joe would write his problem."

The requirements in this situation were a) the ability to parse the emails for contact info; b) the ability to create a new contact if this was not someone currently in the database; c) to relate the new contact to a specific catch-all account; and d) to create a case (and populate fields in the case), then relate the case to the contact.

Below is the example code for creating this email service. Names have been changed to protect the innocent:

   global class CustomEmailToCase implements Messaging.InboundEmailHandler {

// Instantiate variables that we will need for handling this email
public Contact cnt;
public Case sCase;
public String name;
public String role;
public String emailAddress;
public String phone;
public String city;
public String region;
public String country;
public Integer nameIdx;
public Integer roleIdx;
public Integer emailAddressIdx;
public Integer phoneIdx;
public Integer cityIdx;
public Integer regionIdx;
public Integer countryIdx;
public String nameFinal;
public String roleFinal;
public String emailFinal;
public String phoneFinal;
public String cityFinal;
public String regionFinal;
public String countryFinal;

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

if (email.subject =='Email Subject 1' || email.subject == 'Email Subject 2') {
// Set variables that will be used to locate the indexes
name = 'Name: ';
role = 'Role: ';
emailAddress = 'Email: ';
phone = 'Phone: ';
city = 'City: ';
region = 'Region: ';
country = 'Country: ';

// Create the indexes for the beginning of each piece of contact info we need.
// This is the first step in parsing the email to extract the Contact Info
nameIdx = email.plainTextBody.indexOf(name);
roleIdx = email.plainTextBody.indexOf(role);
emailAddressIdx = email.plainTextBody.indexOf(emailAddress);
phoneIdx = email.plainTextBody.indexOf(phone);
cityIdx = email.plainTextBody.indexOf(city);
regionIdx = email.plainTextBody.indexOf(region);
countryIdx = email.plainTextBody.indexOf(country);

// Using the above indexes, find the starting point within the email for each piece of contact info,
// then grab everything from that index to the end of that line and store it in a new variable.
// This is the second part of parsing the email to extract the Contact Info.

nameFinal = email.plainTextBody.substring(
nameIdx + name.length(), email.plainTextBody.indexOf('\n', nameIdx + name.length()));
String[] cntNme = nameFinal.split(' ', 3);
roleFinal = email.plainTextBody.substring(
roleIdx + role.length(), email.plainTextBody.indexOf('\n', roleIdx + role.length()));
emailFinal = email.plainTextBody.substring(
emailAddressIdx + emailAddress.length(), email.plainTextBody.indexOf('\n', emailAddressIdx + emailAddress.length()));
phoneFinal = email.plainTextBody.substring(
phoneIdx + phone.length(), email.plainTextBody.indexOf('\n',phoneIdx + phone.length()));
cityFinal = email.plainTextBody.substring(
cityIdx + city.length(), email.plainTextBody.indexOf('\n', cityIdx + city.length()));
regionFinal = email.plainTextBody.substring(
regionIdx + region.length(), email.plainTextBody.indexOf('\n', regionIdx + region.length()));
countryFinal = email.plainTextBody.substring(
countryIdx + country.length(), email.plainTextBody.indexOf('\n', countryIdx + country.length()));

// Using the email address from above, query the database for a matching Contact.
if (emailFinal.length() > 0) {
Contact [] contactArray = [Select Id, Email, AccountId from Contact where Email =: emailFinal limit 1];

// If the contact is a match, create a case and relate it to the Contact.
if (contactArray.size() > 0) {
sCase = new Case();
sCase.ContactId = contactArray[0].Id;
sCase.AccountId = contactArray[0].AccountId;
sCase.Status = 'New';
sCase.Type = 'BD';
sCase.Origin = 'Email';
sCase.Priority = 'Medium';
sCase.Subject = email.subject;
sCase.Description = email.plainTextBody;
sCase.OwnerId = 'xxxxxxxxxxxxxxxx';

try {
insert sCase;
} catch (System.DmlException e)
{System.debug('ERROR: Not able to create Case: ' + e);

// If the contact is NOT a match, first create the Contact then create a Case related to the contact.

if (contactArray.size() == 0 || contactArray.size() == null) {
cnt = new Contact();
cnt.AccountId = 'xxxxxxxxxxxxxxxx';
cnt.FirstName = cntNme.get(0);
cnt.LastName = cntNme.get(1);
cnt.Email = emailFinal;
cnt.Phone = phoneFinal;
cnt.Title = roleFinal;
cnt.MailingCity = cityFinal;
cnt.MailingState = regionFinal;
cnt.OwnerId = 'xxxxxxxxxxxxxxxx';

try {
insert cnt;
} catch (System.Dmlexception e)
{System.debug('Error: Not able to create new Contact: ' + e);
sCase = new Case();
sCase.ContactId = cnt.id;
sCase.AccountId = 'xxxxxxxxxxxxxxxx';
sCase.Status = 'New';
sCase.Type = 'BD';
sCase.Origin = 'Email';
sCase.Priority = 'Medium';
sCase.Subject = email.subject;
sCase.Description = email.plainTextBody;
sCase.OwnerId = 'xxxxxxxxxxxxxxxx';

try {
insert sCase;
} catch (System.Dmlexception e)
{System.debug('Error: Not able to create new Case: ' + e);

// If no email address is gathered from the email body, write to the debug log, and no actions
// will be performed.
else if (emailFinal == null || emailFinal.length() == 0){
System.debug('No email address was found.');

return result;

} // Close handleInboundEmail ()

//Test Methods

static testmethod void myEmailTest1 () {

//Test #1 - Create New Contact and add Case

// Create a new email, envelope object and Attachment
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
String n = '\n';

env.fromAddress = 'thisEmailIsOk@gooogle.com';
email.subject = 'Email Subject 1';
email.plainTextBody = 'Name: Joe Test'+ n +
'Role: Owner' + n +
'Email: test@email.com' + n +
'Phone: 555-555-5555' + n +
'City: TestCity' + n +
'Region: Georgia' + n +
'Country: USA' + n ;

// call the class and test it with the data in the testMethod
CustomEmailToCase emailServiceObj = new CustomEmailToCase();
emailServiceObj.handleInboundEmail(email, env );

static testmethod void myEmailTest2 () {

//Test #2 - Attaching Case to Matching Contact

Messaging.InboundEnvelope env2 = new Messaging.InboundEnvelope();
// first create and insert contact
// test with a matching contact
Messaging.InboundEmail email2 = new Messaging.InboundEmail();
Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'Test';
c.Email = 'testing@testing.com';
insert c;

// create email body using above email address
env2.fromAddress = 'thisEmailIsOk@gooogle.com';
email2.subject = 'Email Subject 1';
email2.plainTextBody = 'Name: Test Test'+ n +
'Role: Owner' + n +
'Email: testing@testing.com' + n +
'Phone: 555-555-5555' + n +
'City: TestCity' + n +
'Region: Georgia' + n +
'Country: USA' + n ;

//call the class and test it with the data
CustomEmailToCase emailServiceObj2 = new CustomEmailToCase();
emailServiceObj2.handleInboundEmail(email2, env2);

static testmethod void myEmailTest3 () {

//Test #3 - Test with invalid fromAddress

Messaging.InboundEnvelope env3 = new Messaging.InboundEnvelope();

// this from address isn't on our whitelist so it shouldn't trigger any code
env.fromAddress = 'test@test.com';
email3.subject = 'Spam';
email3.plainTextBody = 'Name: Test Test'+ n +
'Role: Owner' + n +
'Email: testing@testing.com' + n +
'Phone: 555-555-5555' + n +
'City: TestCity' + n +
'Region: Georgia' + n +
'Country: USA' + n ;

// test incoming email that is not valid
Messaging.InboundEmail email3 = new Messaging.InboundEmail();

// call the class and test it with the data
CustomEmailToCase emailServiceObj3 = new CustomEmailToCase();
emailServiceObj3.handleInboundEmail(email3, env3);

static testmethod void myEmailTest4 () {

//Test #4 - Test with blank Email field

Messaging.InboundEmail email4 = new Messaging.InboundEmail();
Messaging.InboundEnvelope env4 = new Messaging.InboundEnvelope();

// Use a blank email address
env.fromAddress = 'thisEmailIsOk@gooogle.com';
email3.subject = 'Email Subject 1';
email3.plainTextBody = 'Name: Test Test'+ n +
'Role: Owner' + n +
'Email: ' + n +
'Phone: 555-555-5555' + n +
'City: TestCity' + n +
'Region: Georgia' + n +
'Country: USA' + n ;

// call the class and test it with the data
CustomEmailToCase emailServiceObj4 = new CustomEmailToCase();
emailServiceObj4.handleInboundEmail(email4, env4);

} // Close Class

More Stories By Clint Lee

Clint is a Principal and Founder of The Flywheel Group, a premier provider of innovative solutions to the franchise industry. The FranchiseFlywheel™ application was developed out of the industry's need for a cutting-edge and affordable franchise management tool for franchisors. Spending several years in the franchise industry directing daily activities related to franchise and business development processes, marketing, lead-generation, and contract administration, and an understanding of the needs of peers and colleagues, led to the development of the application. The Flywheel Group is focused on improving best practices and driving change in the franchise industry by introducing industry-leading solutions and working with its clients to re-engineer business processes that will ultimately enhance revenue and improve bottom line efficiency. Clint actively writes and shares his thoughts on the Flywheel Blog.

@ThingsExpo Stories
In his session at @ThingsExpo, Arvind Radhakrishnen discussed how IoT offers new business models in banking and financial services organizations with the capability to revolutionize products, payments, channels, business processes and asset management built on strong architectural foundation. The following topics were covered: How IoT stands to impact various business parameters including customer experience, cost and risk management within BFS organizations.
Here are the Top 20 Twitter Influencers of the month as determined by the Kcore algorithm, in a range of current topics of interest from #IoT to #DeepLearning. To run a real-time search of a given term in our website and see the current top influencers, click on the topic name. Among the top 20 IoT influencers, ThingsEXPO ranked #14 and CloudEXPO ranked #17.
While the focus and objectives of IoT initiatives are many and diverse, they all share a few common attributes, and one of those is the network. Commonly, that network includes the Internet, over which there isn't any real control for performance and availability. Or is there? The current state of the art for Big Data analytics, as applied to network telemetry, offers new opportunities for improving and assuring operational integrity. In his session at @ThingsExpo, Jim Frey, Vice President of S...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
In his session at Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to maximize project result...
The Founder of NostaLab and a member of the Google Health Advisory Board, John is a unique combination of strategic thinker, marketer and entrepreneur. His career was built on the "science of advertising" combining strategy, creativity and marketing for industry-leading results. Combined with his ability to communicate complicated scientific concepts in a way that consumers and scientists alike can appreciate, John is a sought-after speaker for conferences on the forefront of healthcare science,...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
Personalization has long been the holy grail of marketing. Simply stated, communicate the most relevant offer to the right person and you will increase sales. To achieve this, you must understand the individual. Consequently, digital marketers developed many ways to gather and leverage customer information to deliver targeted experiences. In his session at @ThingsExpo, Lou Casal, Founder and Principal Consultant at Practicala, discussed how the Internet of Things (IoT) has accelerated our abilit...
When shopping for a new data processing platform for IoT solutions, many development teams want to be able to test-drive options before making a choice. Yet when evaluating an IoT solution, it’s simply not feasible to do so at scale with physical devices. Building a sensor simulator is the next best choice; however, generating a realistic simulation at very high TPS with ease of configurability is a formidable challenge. When dealing with multiple application or transport protocols, you would be...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Detecting internal user threats in the Big Data eco-system is challenging and cumbersome. Many organizations monitor internal usage of the Big Data eco-system using a set of alerts. This is not a scalable process given the increase in the number of alerts with the accelerating growth in data volume and user base. Organizations are increasingly leveraging machine learning to monitor only those data elements that are sensitive and critical, autonomously establish monitoring policies, and to detect...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, provided an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settl...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
Organizations planning enterprise data center consolidation and modernization projects are faced with a challenging, costly reality. Requirements to deploy modern, cloud-native applications simultaneously with traditional client/server applications are almost impossible to achieve with hardware-centric enterprise infrastructure. Compute and network infrastructure are fast moving down a software-defined path, but storage has been a laggard. Until now.