Email Services in Salesforce
Author
October 1, 2019
Email services in Salesforce are automated processes that use Apex class to process the Attachments, Headers, and Contents of an inbound email. We can associate each email service with one or more Salesforce-generated email addresses through which users can send messages for processing.
Use of Apex In Email Services
Apex class act as a medium to process incoming email messages. When we set up an email service in salesforce, we need to generate a special email address in which salesforce will receive emails.
We can use Apex to receive and process email and attachments from the external system to the Salesforce Platform.
The email that is sent by the external user is received by the Apex email service in salesforce and processed by Apex classes that use the Inbound Email object.
Apex email service creates an Inbound Email object that contains the contents and attachments of that email which has been sent by the external user.
We can use the Apex classes to implement the Messaging. Inbound Email Handler interface to handle the inbound email message.
How Email Services in Salesforce Works?
Email Services in Salesforce uses predefined Classes
- Inbound Email Handler
- Inbound Email Binary Attachments
- Inbound Email Inbound Envelope
- Inbound Email Result
Methods in Inbound Email Properties
We can access an Inbound Email object to get the headers, contents, and attachments of inbound email messages, and perform many functions.
- Binary Attachments
- CC Addresses
- From Address
- From Name
- Headers
- HTML Body
- HTML Body Is Truncated
- In Reply To
- Message-Id
- Plain Text Body
- Plain Text Body Is Truncated
- Reply To
- Subject
- Text Attachments
- To Addresses
Inbound Envelope Properties
From Address – The name that appears in From field of the envelope,
To Address – The name that appears in the field of the envelope.
Messaging Inbound Envelope
The object of this class keeps the information of the envelope (From address and to address) associated with inbound email.
Messaging Inbound Email Result
The Inbound Email Result is used to return the result of an email service. To access email services in Salesforce we will activate email service.
Example of Email Service – Change the Opportunity Stage to ‘Closed Won’.
- Subject of Email: USH Youth Programs Number – 0062S00000t7FCn has been Placed.
- Email Body: Order Number is #9871 Please Use it as a Reference.
Apex Class
@TestVisible
global class OrderNumber implements Messaging.InboundEmailHandler {
@TestVisible
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String inputString= email.subject;
String reser= email.plainTextBody;
String sub=inputString.substringAfter(‘Number’);
String sub2=sub.substringBefore(‘has’);
sub2 = sub2.trim();
String r1=reser.substringAfter(‘is’);
String r2=r1.substringBefore(‘Please’);
r2=r2.trim();
r2=r2.replace(‘\n\n’,”);
Opportunity op = [Select id, StageName from Opportunity where id=:sub2];
Opportunity op1 = [Select id,Reservation_Number__c from Opportunity where id=:sub2];
// System.debug(r1);
//System.debug(op1);
op.StageName = ‘Closed Won’;
op1.Reservation_Number__c = r2;
update op;
update op1;
result.success = true;
System.debug(result);
return result;
}
}
Test Class
@isTest(SeeAllData=True)
private class OrderNumberTest {
static testMethod void validateHelloWorld() {
Test.startTest();
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
Opportunity op = new Opportunity (Id=’0062v00001CLZcIAAX’,StageName=’Closed Won’);
update op;
Opportunity op1 = new Opportunity (Id=’0062v00001CLZcIAAX’,Reservation_Number__c=’#9871′);
update op1;
email.subject= ‘USH Youth Programs Number – 0062S00000t7FCn has been Placed’;
email.plainTextBody= ‘ Order Number is #9871 Please Use it as a Reference ‘;
env.fromAddress=’xyz@mirketa.com’;
Test.stopTest();
OrderNumber ac = new OrderNumber();
ac.handleInboundEmail(email, env );
}
}
After creating Above Apex Class follow the steps mentioned below.
- Go to Email Services in Your Org.
- Click New to define a new email service.
- Select the above apex class and add the email address from where to accept the request.
- Activate the service.
- Next, click the “Save and New Email Address”.
- Enter the details that are required.
- Click the “Save” button.
- Salesforce will create an email.
- Copy that email and send the email to that email generated by Email Service.
Recent Posts
-
Leveraging AI to Enhance Sales Effectiveness13 Mar 2025 Use-case
-
Revolutionizing Manufacturing with AI: Predictive Maintenance, Supply Chain Optimization, and More11 Mar 2025 E-Book
-
NetSuite for Manufacturing: Streamlining Operations and Solving Key Challenges07 Mar 2025 Blog
-
How to Build Your First Agent in Salesforce Agentforce24 Feb 2025 Blog
-
ERP vs Salesforce Revenue Cloud: Which One is Right for Your Business?24 Feb 2025 E-Book
-
Revolutionizing Manufacturing with Salesforce: A Playbook for Efficiency & Growth18 Feb 2025 E-Book
-
Salesforce 2025 Game-Changing Trends You Need to Know28 Jan 2025 Blog
-
Agentforce 2.0: Everything You Need to Know About the Latest Update22 Jan 2025 Blog
-
The Ultimate Guide to NetSuite Development: Tools and Techniques10 Jan 2025 Blog
-
How Salesforce Nonprofit Cloud Transforms Fundraising Strategies10 Jan 2025 Blog
-
The Impact of Salesforce Development Partners on Small and Medium Businesses08 Jan 2025 Blog
-
Key Questions to Ask When Hiring a NetSuite Development Partner08 Jan 2025 Blog
-
Salesforce Agentforce Demystified: Your Essential Guide08 Jan 2025 Blog
-
Salesforce and NetSuite Integration: Driving Business Efficiency with Precision06 Jan 2025 Blog
-
Everest Group has positioned Mirketa as an Aspirant in the report24 Dec 2024 Press Release
-
Salesforce Einstein20 Dec 2024 E-Book
-
Order to Cash Cycle with NetSuite20 Dec 2024 E-Book
-
Empower Your Marketing Strategy with Salesforce Marketing Cloud's Automation Studio Activities13 Dec 2024 Blog
-
Salesforce CPQ for Subscription-based Businesses10 Dec 2024 Blog
-
Unleashing the Magic of Einstein Prediction Builder10 Dec 2024 Blog
-
Customized Templates and Branding with Salesforce Experience Cloud10 Dec 2024 Blog
-
Unleashing the Power of Real- Time Reports and Dashboards in NPSP10 Dec 2024 Blog
-
Top 4 Salesforce Automation Tools in 202409 Dec 2024 Blog
-
Salesforce Service Cloud Implementation: The Ultimate Guide09 Dec 2024 Blog
-
Salesforce CRM Implementation Partner Enhancing Automation in Healthcare09 Dec 2024 Blog