Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles

Blog

Email Services in Salesforce

By |2020-07-16T08:46:36+00:00October 1st, 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
Email Services in Salesforce uses predefined Classes

  1. Inbound Email Handler
  2. Inbound Email Binary Attachments
  3. Inbound Email Inbound Envelope
  4. 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.

  1. Go to Email Services in Your Org.
  2. Click New to define a new email service.
  3. Select the above apex class and add the email address from where to accept the request.
  4. Activate the service.
  5. Next, click the “Save and New Email Address”.
  6. Enter the details that are required.
  7. Click the “Save” button.
  8. Salesforce will create an email.
  9. Copy that email and send the email to that email generated by Email Service.

5 Comments

  1. Satwik Srivastava July 7, 2020 at 11:37 am - Reply

    Valuable…Thanks for the info !!

    • Mirketa Inc August 12, 2020 at 9:20 am - Reply

      Thanks

  2. Vitalii Tatsenko May 10, 2022 at 12:30 pm - Reply

    Thank you!
    This is perfect article about Email Service in SalesForce

  3. Kumar May 21, 2022 at 1:44 am - Reply

    Good example. Is it possible to map the random generated email to an user email ? The reply should be sent to User email that is mapped

  4. Anshu Mishra July 27, 2022 at 6:37 pm - Reply

    Is there a way to use an external email service ?

Leave A Comment Cancel reply