How to convert leads using apex in salesforce?

How to convert leads using apex in salesforce?

Lead Conversion Using Apex


3 min read

In Salesforce, Lead is one of the most important objects. We create a lead record to track information about potential customers who are interested in buying our products/services. The lead conversion process in Salesforce is a critical step in the sales process because it allows you to track the progress of your leads and identify the most promising opportunities. These leads can be converted to account & contact and optionally opportunity using convert quick action from lead records detail page. But what if we are designing a customized solution and have a requirement to convert those leads using Apex? That is what we will learn in this article.

What is Lead Conversion?

In Salesforce, a lead is a person or company who has shown interest in your product or service but is not yet a customer. Lead conversion is the process of converting a lead into a customer by creating a new account, contact, and opportunity record from the lead record.

How to convert Lead using Apex?

Let's write the apex code to convert leads.

The below image is the lead record that I have created in my org. We will be converting this lead record into account, contact & opportunity.

Copy the below code and execute it from the anonymous window.

//Id of the lead to be converted
String leadId = '00Q5g00000KtB19EAF';

//Creat a lead convert object and pass the id and status 
Database.LeadConvert lc = new database.LeadConvert();
lc.setConvertedStatus('Closed - Converted');

//Pass the lead convert object to convertlead object so it can be converted.
Database.LeadConvertResult lcr = Database.convertLead(lc);

//Debug the AccountId, ContactId & OpportunityId after the lead is converted

In the above code first, we create a variable to store the id of the lead to be converted. After that we convert the LeadConvert object and pass the lead id of the lead to be converted and what will be the status of the lead once it is converted.

Then we pass the LeadConvert object to convertLead for converting the lead into account, contact & opportunity.

If you execute the above code from the anonymous window you will see the following output on debug logs.

Once you execute the above code and refresh the lead record you will be redirected to the contact record which is created after the lead conversion.

Here is the screenshot of the contact record created after i converted the lead. You can clearly see the account, contact & opportunity information.

If you don't want to create an opportunity while converting leads you can pass createOpportunity as True to the setDoNotCreateOpportunity(createOpportunity) method.

There are lots of methods in the LeadConvert class that you can explore from here.

Also, check out the below video on How to convert leads using Apex in Salesforce.


I hope I was able to explain to you how to convert leads using apex. If this article helped you then consider buying me a coffee.

Please don't forget to like, comment and share and you can also check out my other articles. Also, don't forget to subscribe to my newsletter so you can be notified about new articles.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here.

Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!