How to create a lead from an email?

I am looking for the easiest option to create a Lead using an email. Ideally I would send an email to a predefined email address, and it would automatically create a Lead in my organisation.


One of the packaged apps mentioned in other answers may well be the easiest option, but it’s pretty straightforward to create an Email Service in Apex. The sample code in the docs creates a Contact and a Task; here is code to create a Lead instead:

global class CreateLeadExample implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
    // Create an InboundEmailResult object for returning the result of the  
    // Apex Email Service 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String myPlainText= '';

    // Add the email plain text into the local variable  
    myPlainText = email.plainTextBody;

    // Check for existing leads with this email address   
    Lead[] leads = [SELECT Id, Name, Email
      FROM Lead
      WHERE Email = :email.fromAddress];

    if (leads.size() == 0) {
      // New Lead object to be created - set LastName and Company to
      // dummy values for simplicity
      Lead newLead = new Lead(Email = email.fromAddress, 
        LastName = 'From Email', 
        Company = 'From Email');

      // Insert a new lead
      insert newLead;    

      System.debug('New Lead record: ' + newLead );   
    } else {
      System.debug('Incoming email duplicates existing Lead record(s): ' + leads );    

    // Set the result to true. No need to send an email back to the user      
    // with an error message   
    result.success = true;

    // Return the result for the Apex Email Service 
    return result;

Source : Link , Question Author : Mark Kofman , Answer Author : metadaddy

Leave a Comment