Friday, January 8, 2016

Apex Inbound email services

Apex Inbound email services:


This is an automated process which uses apex classes to process the contents, headers and attachments of inbound email.

We can create an email service which will automatically crates the contact or account or any record based on the information provided in the email .
We can also use Apex class to send individual or mass email.
We can use apex to send email and process incoming email to salesforce.
Two types of email 
  1. Inbound
  2. Outbound

Inbound email:

When any email coming to salesforce we can execute apex logic to process the incoming message.
We need to go through 2 steps to configure inbound email service
  1.  Build an inbound email handler apex class for processing the email.
  2.  Configure the email service, binding it to the apex class

Step 1:

To build an inbound email handler , we need to implement interfaceMessaging.InboundEmailHandler.

Ex:
Global class InboundEmailReceive implements Messaging.InBoundEmailHandler
{

}

This interface is having one method, which we need to give the definition in the implemented class.
The interface Method is:

Messaging.InboundEmailResult handelInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope)

Parameters in this method defined as:
           Email: available message contains 
          Envelope: stores the to and from address
We will write a class to process the incoming email and based on that email subject we will create account and from CC address we will create contact , any attachmnets in the mail will be added in attachment and content will be added in the notes.
So we need to have a class which will implement interface Messaging.InboundEmailHandler and will give the definition to the methodhandelInboundEmail()
Sample code:

global class InboundEmailProcess implements Messaging.InboundEmailHandler {
global messaging.InboundEmailResult handelInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
      Account acc ;
      Boolean accExist = false;
      Messaging.InboundEmailResult result = new Messaging.inboundEmailResult();
      Integer count = [select count() from account where name =:email.subject];
      If(count>0)
           accExist = true;
      if(!accExist){
          acc = new Account();
          acc.name = email.subject;
          insert acc;
          for(string ccAddress : email.ccAddresses){
              contact con = new contact();
              con.email = ccAddress;
              con.FirstName = ccAddress.substringBefore(‘.’);
              con.LastName = ccAddress.subStringAfter(‘.’).subStringBefore(‘@’);
              insert con;
          }
       List<Attachment> attachList = new list<Attachment>();
       For(Messaging.inboundemail.TextAttachment textAtt : email.textAttachments){
           Attachment attach = new Attachment();
           Attach.name = textAtt.filename;
           Attach.body = Blob.valueof(textAtt.body);
           Attach.parentid = acc.id;
           attachList.add(attach);
      }
     For(Messaging.inboundemail.BinaryAttachment bnryAtt : email.binaryAttachments){
         Attachment attach = new Attachment();
         Attach.name = bnryAtt.filename;
         Attach.body = bnryAtt.body;
         Attach.parentid = acc.id;
        attachList.add(attach);
     }
     If(!attachList.isempty()){
        Insert attachList;
     }
  }
  Return result;
 }
}

Step 2:

Configure the email service

Now we need to define email service.
Go to setup--> Develop--> Email service --> new Email service
Here enter the value for the field like
Email Service Name – any name as you like
Apex class –the class which we have created now
Accept Attachments – select any one from thelist(none/text/binary/All)
Based on your preference you can select the values for any field or you can leave with all default setting.
Click on save.
Once we created email service , we need to generate the email address for this service on which all incoming message will be received to process.
Now click on the “New Email Address” from your email service page.
Enter Email address field value as anything you want.
select the Active check box,
select the context user(based on which user permission , this email will process),
in Acccept email form, specify from which domain or email you want to accept email, if you will keep blank, then from all address email can be received.
Now a new email id will generate from salesforce and send the email to this email id and check based on your subject Account will create and based on CC address Contacts will create and attachments will add in attchement section of the Account.


Note:  please feel free to comment your view in comment box.

5 comments:

  1. Great !! Could you please elaborate with live scenario .

    ReplyDelete
  2. it's really helpful could you please send one scenario explanation for the javascript remoting..

    Kind regards,
    Vishnuvardhana

    ReplyDelete
    Replies
    1. Thanks for your comment. I will post example with explanation of JS remoting in my next blog.Stay tuned.

      Delete
    2. Hay Debi enjoying your Blog. Keep the great work Man !!!

      Delete