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 Administrator in 2027: A Step-by-Step Career Roadmap
Want to become a Salesforce Administrator? Get the complete 2026 roadmap skills, certification, salary & career path. Start your…
Salesforce Salesforce Admin Self Study vs Live Training: How to Choose the Right Path
Compare Salesforce Admin self-study and live training by cost, duration, flexibility, mentorship, and career support. Find the…
Salesforce How a Salesforce Admin Certification Shapes Your Career Growth
Wondering if a salesforce admin certification is worth it? See how it shapes hiring, salary, and long term salesforce admin…
Salesforce Salesforce Admin vs Developer Salary: Which Path Actually Pays More?
Salesforce admin vs developer salary: developers earn 15–25% more. See entry, mid and senior pay, why the gap exists, and how…