ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Email Services in Salesforce with a simple example

Home / Article & Blogs / Salesforce / Email Service / Email Services in Salesforce with a simple example

Email Services in Salesforce with a simple example

By Haseeb Khan inEmail Service, Salesforce

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!!!
29
Like this post
4 Posts
Haseeb Khan

Search Posts

Archives

Categories

Recent posts

Top 5 Factors to evaluate before choosing a Salesforce Integrator

Top 5 Factors to evaluate before choosing a Salesforce Integrator

Significance of UI/UX Design

Significance of UI/UX Design

Cyber-security in an uncertain world

Cyber-security in an uncertain world

The world of AR and VR

The world of AR and VR

The in-and-out of ML

The in-and-out of ML

  • Previous PostRoll-up Summary without a Master Detail Relationship
  • Next PostToo Many Verification Code requests on Salesforce login

Related Posts

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy