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);
           
            memb.add(mem);
           
            insert memb;   
            
            System.debug('New member: ' + memb);  
        }
           
        catch (Exception e) {
            System.debug('Error is: ' + e);
        }  
         
        result.success = true;
        
        return result;
    }
}


Note:

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

https://help.salesforce.com/HTViewHelpDoc?id=code_inbound_email.htm&language=en_US

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

http://www.salesforce.com/us/developer/docs/apexcode/

No comments:

Post a Comment