December 28, 2012

Inbound Email - Creating record using email in Salesforce

You can use Apex to receive and process email and attachments. The email is received by the Apex email service and processed by Apex classes that utilize the InboundEmail object.

1. Go to Setup --> App Setup --> Develop --> Apex Classes.

Create a class that implements the interface "Messaging.InboundEmailHandler".

Sample code:

global class createMemberInbound implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        String myPlainText= '';    
        myPlainText = email.plainTextBody; 
        List<Member__c> memb = new List<Member__c>();
        try {
            Member__c mem = new Member__c(Ext_Id__c = myPlainText, Name = email.Subject);
            insert memb;   
            System.debug('New member: ' + memb);  
        catch (Exception e) {
            System.debug('Error is: ' + e);
        result.success = true;
        return result;


For other attributes of Messaging.InboundEmail, check the below link

2. Go to Setup --> App Setup --> Develop --> Email Services.

3. Click "New Email Service" button.

4. Fill in the details and click "Save" button.

5. Click "New Email Address" button.

6. Fill in the details and click "Save" button.

7. Copy the generated Email address.

8. Compose an email with to address generated email address.

9. Check whether the record has been created in you Salesforce organization.

For more info, kindly visit the below link


  1. my record is not created. will u please give me reasons why is it not doing this

    1. May be it is failing with required fields or validation rules. Put debug log to find the issue.