About this question
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.
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.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on May 1, 2023
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;Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Become a Salesforce Admin With No Experience
No Salesforce Admin experience? Learn how to build real skills, create projects, and land an entry-level Salesforce Admin job…
Salesforce How Long Does It Take to Become a Salesforce Developer?
Wondering how long to learn Salesforce Developer? Most beginners need 6–12 months. See the study time, stage-by-stage roadmap and…
Salesforce How Long Does It Take to Become a Salesforce Admin?
Wondering how long to learn Salesforce Admin? Most beginners need 3–6 months. See the study time, stage-by-stage roadmap and…
Salesforce How to Prepare for Salesforce Developer Certification: A Practical Guide
A step-by-step guide to Salesforce Developer certification - what to study for PD1, how PD2 fits in, and how to know you're ready…