What is the conversion process of email to lead salesforce?

542    Asked by AndreaBailey in Salesforce , Asked on May 1, 2023

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 Salesforce.com organisation.

Answered by Diya tomar

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 email to Lead salesforce instead:

            global class CreateLeadExample implements <a href="https://www.janbasktraining.com/blog/inbound-email-service-salesforce/">Messaging.InboundEmailHandler</a> { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ // Create an InboundEmailResult object for returning the result of the // <a href="https://www.janbasktraining.com/blog/apex-email-salesforce/"><a href="https://www.janbasktraining.com/blog/apex-email-salesforce/"><a href="https://www.janbasktraining.com/blog/apex-email-salesforce/"><a href="https://www.janbasktraining.com/blog/apex-email-salesforce/">Apex Email</a></a></a> Service</a> 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;


Your Answer

Interviews

Parent Categories