Email Services in Salesforce with a simple example

Email Services in Salesforce with a simple example

Email Services is a feature of salesforce.com where, the user can assign an Apex Class that implements the Messaging.InboundEmailHandler interface to a configuration which allows you to process the email contents, headers and attachments. Using this Information, you can cater a variety of requirements . Listing few, create a new contact if one does not exists with that email address, receive job applications and attached the person’s resume to their record.

Email services are basically an option to a developer to develop a functionality , where an email can be received at a particular address and some operations can happen on the data retrieved from the email. These operations are done by the Apex Code, thus giving us a spectrum of options that fulfils requirements.

Explaining few,

A track of contacts can be kept , once the email is received the contacts are checked for the email address and if not a new contact is created and the attachments in the mail can be added to the new contact record.

In a Human resources management system, All job posting can have a email to it . All the job applications can be received via email and the subject can become the Job title the applicant is applying to. The email service automatically receives the email and creates a job application record using the subject of the email to categorise between job applied for and the resumes can be attached to the job application record as attachments.

Now to start using them,

You can Navigate to Salesforce Email Services using this path : Setup -> Develop -> Email Services,

OR just type Email services in the Quick Find Box.

Click the “New Email Service” button to get started and fill out the form.

The Form has a number of Options, Including Name, Apex Class Etc. Some Options are very handy, “Enable Error Routing” will send the inbound email to an alternative email address when the processing fails. You can can also specify email address to accept mail from.

Note : Make Sure you create a Basic Class that implements the Messaging.InboundEmailHandler interface that can be assign as the class here.

After you save the new email service, you will need to scroll down to the bottom of the page and create a new email address for the service. An email service can have multiple email addresses and therefore process the same message differently for each address. When you create a new email service address you specify the “Context User” and “Accept Email From”. The email service uses the permissions of the Context User when processing the inbound message. So you could, for example, have the same email service that accepts email from US accounts and processes them with a US context user. After the form is submitted the Platform will create a Unique Email Address, To which the email has to be sent. For Example,

test@h-146i8zqbmxebmwfhxj8mdwp1kt96y38ko1b2a9x5rsxea0w8mh.9-10wvleae.ap1.apex.salesforce.com .

Now that we are done with configuring the Email Services, We can move to write the Apex Class as per Requirements.

Scenario :  Create a Contact using the Information in the email and add attachments.

[sourcecode language=”java”]
global class EmailService implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
list<Attachment>att = new list<Attachment>();
Contact con = new contact();
if(email.fromname!=null||email.fromname!=”){
con.FirstName = email.fromname.substring(0, email.fromname.indexOf(‘ ‘));
con.LastName = email.fromname.substring(email.fromname.indexOf(‘ ‘));
}
con.email = envelope.fromaddress;
if(email.plaintextbody!=null||email.plaintextbody!=”)
con.description = email.plaintextbody;
if(email.subject!=null||email.subject!=”)
con.title = email.subject;
insert con;

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0;i<email.binaryAttachments.size();i++) {
Attachment attachment = new Attachment();
attachment.ParentId = con.Id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
att.add(attachment);
}
insert att;
}

if (email.textAttachments != null && email.textAttachments.size() > 0) {
for (integer i = 0; i < email.textAttachments.size(); i++) {
Attachment attachment = new Attachment();
attachment.ParentId = con.Id;
attachment.Name = email.textAttachments[i].filename;
attachment.Body = blob.valueOf(email.textAttachments[i].body);
att.add(attachment);
}
insert att;
}
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
return result;
}
}
[/sourcecode]

Similarly you can have any sort of code in
the apex class, Depends solely on what the 
requirement is. 
Basically the lines of code can use the 
information available and perform some 
operations based on the requirements. 
Happy coding!!!

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

Salesforce CRM Insights
From Data to Action: Tableau Agent’s Role in Shaping Salesforce CRM Insights
future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top