+ Start a Discussion
ankur khattriankur khattri 

email to lead not working properly

global class EmailReceive implements Messaging.InboundEmailHandler
 {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
    
    String[] emailBody = email.plainTextBody.split('\n');
    system.debug('------------emailbody------'+emailBody);
    //String firstName = emailBody[0].substring(5);
    String lastName= emailBody[0].substring(2);
    String phoneNumber = emailBody[0].substring(6);
    String city = emailBody[0].substring(6);
    String status = emailBody[0].substring(3);
    String company= emailBody[0].substring(2);
    Lead[] newLead = new Lead[0];
     try {
     if (newLead .size() == 0)
    {
       newLead.add(new Lead(LastName = lastName,Phone = phoneNumber, City = city,Company=company ));
       insert newLead;        
    }
    }
   catch (QueryException e) {
       
   }
   
   result.success = true;
   return result;
  }
}
SFDC GuestSFDC Guest
Hi ankur khattri,

Please use the below code.

global class Email2Lead implements Messaging.InboundEmailHandler
 {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
    
    String[] emailBody = email.plainTextBody.split('\n');
    system.debug('------------emailbody------'+emailBody);
    //String firstName = emailBody[0].substring(5);
    String lastName= emailBody[0].substring(2);
    String phoneNumber = emailBody[0].substring(6);
    String city = emailBody[0].substring(6);
    String status = emailBody[0].substring(3);
    String company= emailBody[0].substring(2);
    Lead[] newLead = new Lead[0];
     try {
     if (newLead .size() == 0)
    {
       newLead.add(new Lead(LastName = lastName,Phone = phoneNumber, City = city,Company=company ));
       insert newLead;        
    }
    }
   catch (QueryException e) {
       
   }
   
   result.success = true;
   return result;
  }
}

Configuring Email Services in Salesforce:

Go to Set up -> Develop -> Email Services.
Click on New Email Service button.
Give a Service Name.
Select the Apex Class we created in above step.
Check Active to TRUE and click on Save.


Please mark it as best answer if it solves your problem.

Thank You.
ankur khattriankur khattri
i used the certain value in the body of the mail LastName: sandy Phone: 1111111111 Status:Closed - Converted Company: cyno
ankur khattriankur khattri
the output is not correct
SFDC GuestSFDC Guest
Hi,
Please use the below code.

global class Email2Lead 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<Lead> leadList = new List<Lead>();
            
        try {
            Lead lead = new Lead(Description = myPlainText, Name = email.Subject);
            
            leadList.add(lead);
            
            insert leadList;    
             
            System.debug('New lead: ' + leadList);   
        } 
            
        catch (Exception e) {
            System.debug('Error is: ' + e);
        }   
          
        result.success = true;
         
        return result;
    }
}

Thank You.
ankur khattriankur khattri
nor i am getting a error message nor lead is created.
SFDC GuestSFDC Guest
Hi,
In subject of email - write the lead name
in body of email - write description.

Please mark it as best answer if it helps you,
Thank You.