Sunday 21 June 2015

Salesforce Inbound Email Service


First create Apex classes that implement the Messaging.InboundEmailHandler interface.

global class myHandler implements Messaging.InboundEmailHandler {
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
 Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          return result;
      }
  }

Controller

global class TestEmailServices implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
    {
        //Initialize the variables
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        //Check whether email subject startsWith NEW: or not
        if(email.subject.startsWith('NEW:'))
        {
            Case objCase = new Case(Subject = email.subject., Description = email.body, Status = 'New');
            insert objCase;//insert the task
            //create task for newly created task with same subject and email body
            Task objTask = new Task(Subject = email.subject, Description =  email.body , Status='Not Started', whatId = objCase.Id );
            insert objTask;
            esult.message = 'Successfully created Case and Task';
            result.success = true;
            return result;
        }
        else
        {
result.message = 'Please try again.';
result.success = true;
return result;
        }
}
}

This Apex class will create a case an task with email subject and email body.
And return a message to user.

IN Setup part

First Goto Setup --> Develop --> Email Service

Create new email service and choose this apex class.

and save and new Email address

Here you can provide any email address :- example
Click on active and Context User 
II
In Accept email from - You can set any domain or any email address like example@gmail.com or gmail.com

then save it.

Below you can find the addresses
like
example@1kz9xrgxvig77xehrsq4duu3puip1c1r38wgk8t03f7mjdcqwr.9-vzoieaq.ap1.apex.salesforce.com

Now Activate your email service.
Open your gmail and send an email to above address with subject starts with NEW:

Done.




No comments:

Post a Comment