Wednesday 15 April 2015

EMAIL TO APEX USING EMAIL SERVICES IN SALESFORCE



By using Email Services in sales force we can invoke an apex class 
 
By sending an email from any email service(gamil,yahoo..etc) to sales force generated email address it will invoke the apex class 

First we have to create an email service in sales force it will generated an email address 
 
Follow the below steps to create an email service and apex class in sales force

Step 1 : Create an apex class to executive when email receive 


 Ex: Below is the sample code to insert a record in contact (Data got from email body and subject)

global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail      email,Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult resultData = new Messaging.InboundEmailresult();

if((email.subject ! = null && email.plainTextBody != null){

      Contact con = new Contact();
         con.LastName = email.fromName;
         con.Email = email.fromAddress;
         con.Description = email.plainTextBody;
         con.Title = email.subject;
      insert con;

if(email.textAttachments != null){
      for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
             Attachment attachment = new Attachment();
                 attachment.Name = tAttachment.fileName;
                 attachment.Body = Blob.valueOf(tAttachment.body);
                 attachment.ParentId = con.Id;
            insert attachment;
      }
}

  if(email.binaryAttachments != null){
        for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
              Attachment attachment = new Attachment();
                 attachment.Name = bAttachment.fileName;
                 attachment.Body = bAttachment.body;
                 attachment.ParentId = con.Id;
             insert attachment;
      }
  }
}
      resultData.success = true;
      return resultData;
   }
}

Step 2 : After creation of sales force apex class follow below steps to create email service 


Go to SETUP > BUILD > DEVELOPE > EMAIL SERVICES > NEW EMAIL SERVICES



Fill the form

1. Email Service Name : give any name

2. Apex Class : Select a class which you want to executive when email send

3. Accept Attachments : If you want add attachments then select which type of attachment your adding

4. Accept Email From : List the email addresses and domains from which you want this email service                                       to receive emails.                       
Separate multiple entries by commas. For example: george@mycompany.com, yahoo.com, gmail.com.

Leave this field blank if you want the email service to receive email from any email address or domain.
 
5. Active : Check if you want to use this service

6. Route Error Emails to This Email Address : If error occurs while sending, if you want know error ,     give your email address to receive error

7. Save

Step 3 : After saving the record create new email address under Email Addresses


1. Email address : Don't change any thing

2. Accept Email From : Enter email addresses or domain names that are authorized to send emails to      the service.

Separate multiple entries by commas. Examples: george@mycompany.com, yahoo.com, gmail.com.

NOTE: the addresses entered in this field must be the same as or fully qualify addresses or domains allowed by the service as shown above. For example, if the service's list above only include mycompany.com then the list entered below must be of the form user1@mycompany.com, user2@mycompany.com, etc.

3. Save

After saving it will generate an new email address

Step 4 : Copy the email address generated in step 3 and send an email from your email(gmail,yahoo,....etc)  


Then go to the contact object and check it record is create or not

NOTE : wait for few minutes it will take some time to insert a record in sales force