salesforce email service

salesforce email service

Salesforce Email service is automated process that use Apex class to process inbound email. When we set up an email service, we need to generate a special email address in which salesforce will receive your emails. We also need to create one apex class to implement Messaging.InboundEmailHandler interface . The email services will then invoke a handleInboundEmail method on the Apex class whenever an email is received on the email address.

salesforce email service example

Lets create very simple email service which will create contact when an email is sent to salesforce.

Steps to create email service

  1. First of all we need to create Apex class that implement the Messaging.InboundEmailHandler interface. Its necessary to implement Messaging.InboundEmailHandler interface. Using the handleInboundEmail method in that class, you can access an InboundEmail object to retrieve the contents, headers, and attachments of inbound email messages, as well as perform many functions. This class will handle email message.
    global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {
        global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
                                                        Messaging.InboundEnvelope envelope) {
            Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
            String subToCompare = 'Create Contact';
            List<Attachment> attachmentList = new List<Attachment>();
            if(email.subject.equalsIgnoreCase(subToCompare)) {
                Contact c = new Contact();
                c.LastName = email.plainTextBody;
                insert c;
                
                // Save attachments, if any
                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 = c.Id;
                        attachmentList.add(attachment);
                    }
                }
    
                //Save any Binary 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 = c.Id;
                        attachmentList.add(attachment);
                    }
                }
    
                if(!attachmentList.isEmpty()){
                    insert attachmentList;
                }
            }
            result.success = true;
            return result;
        }
    }
    

     

  2. Create email service:
    Go to setup -> Email Service -> New Email Service. Add following details as per this imagesalesforce email service

    Click on Save or Save and New email address.

  3. Generate an email address from email service record
    Create new email address and add all details as per this image:salesforce email service email address

     

    Click on save. New email address will be created in related list. We need to send email to generated email address to test email service. You can copy email address from email service related list so that we can use it for testing.

  4. Testing email service by sending email:
    For testing send an email to this email id with subject ‘Create Contact’ and add some body which will be saved in last name. We can also add attachment(its optional) in email. As contact last name is required field on contact object, so it is necessary to enter text body in email.

For more details about email service please this link from salesforce.

Happy coding ­čÖé

Permanent link to this article: http://www.sfdcpoint.com/salesforce/salesforce-email-service/