Automating Lead Conversion in Salesforce Using Apex Batch


In the world of customer relationship management (CRM), Salesforce stands tall as a prominent platform for managing and nurturing leads. Converting leads into valuable accounts, contacts, and opportunities is a critical step in the sales process. Salesforce provides various ways to handle this, and one of the most powerful methods is through Apex code, particularly using an Apex batch. In this blog post, we'll explore how to meet a specific client requirement: automatically converting leads with a "Conference" lead source into accounts, contacts, and opportunities using an Apex batch.

Client Requirement:

Our client has a clear requirement - all leads with the lead source set to "Conference" need to be automatically converted into Salesforce accounts, contacts, and opportunities. This automation will save time, reduce manual effort, and ensure consistency in lead conversion.

Why an Apex Batch?

To automate the lead conversion process, we'll use an Apex batch class. Apex is a powerful programming language that extends Salesforce's capabilities, and an Apex batch allows for processing large sets of data asynchronously. This is particularly useful when working with large lead datasets, ensuring that the conversion process does not impact system performance.

Building the Solution:

Let's break down the steps to achieve this automated lead conversion:

1. Create an Apex Batch Class:

We start by creating an Apex batch class. This class will define how to retrieve and process the leads.

2. Define the Query:

In the batch class, we define a SOQL (Salesforce Object Query Language) query to retrieve leads with the lead source set to "Conference." This query specifies the criteria for selecting the leads to be converted.

3. Execute Method:

Within the execute method of the batch class, we create a `Database.LeadConvert` object for each selected lead and set the necessary conversion details. This includes specifying the associated account, contact, opportunity, and other conversion parameters.

4. Convert Leads:

We use the `Database.convertLead()` method to convert the selected leads. The system processes these conversions asynchronously, ensuring that large lead datasets can be converted without causing performance issues.

5. Finish Method:

The finish method can be used for any post-processing tasks or cleanup.

The Apex batch code is included below:

Batch Code

    global class LeadConvert implements Database.Batchable {
    global Database.QueryLocator start(Database.BatchableContext BC) {  
        String query = 'SELECT Id, Name, Converted__c, 
        LeadSource FROM Lead where LeadSource = \'Conference\' AND isConverted = False';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List ldList) {
        String convertedLabel = [SELECT Id, 
        MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel;
        List listOfLeadToConvert = new List();
        for(Lead leadRecord:ldList){
            Database.LeadConvert leadConvert = new Database.LeadConvert();
        if(!listOfLeadToConvert.isEmpty()) {
               convertResults = Database.convertLead(listOfLeadToConvert);
    global void finish(Database.BatchableContext BC) {
           system.debug('Batch Running');

Schedule Batch Class


public class LeadConvertBatchSch implements Schedulable{
    public void execute(SchedulableContext sc){
       LeadConvert leadObj = new LeadConvert();

Test Class

public class LeadConvertTest {
    public static void LeadMethod(){
        Lead ld = new Lead();
        ld.FirstName = 'Anurag';
        ld.LastName =  'Tripathi';
        ld.Company = 'Astrea IT Services';
        ld.LeadSource = 'Conference';
        insert ld;
    public static void testBatchExecuteMethod()
        LeadConvert leadObj = new LeadConvert();
        //System.schedule('Schedule Job Name 1',  '0 00 * * * ?', 
        new LeadConvertBatchSch());

Testing and Debugging:

Before deploying the Apex batch to the production environment, it's essential to thoroughly test it in a Salesforce sandbox. This includes testing different scenarios, handling errors, and verifying that the conversion process works as expected. Debugging tools, such as the Developer Console, are invaluable for identifying and resolving issues.

Testing Example

Lead with LeadSource Conference

Figure 1: Lead with LeadSource: Conference

respective Account

Figure 2: Lead gets converted to respective Account, Contact and Opportunity

Lead After Conversion to Contact

Figure 3: Lead After Conversion to Contact

Conversion To Opportunity

Figure 4: Lead After Conversion To Opportunity

Scheduling the Batch:

Once the batch is tested and ready, it can be scheduled to run automatically at specified intervals. Salesforce provides a user-friendly interface for scheduling jobs, making it easy to set up recurring lead conversions without manual intervention.


Automating lead conversion in Salesforce using an Apex batch can significantly streamline the sales process, save time, and ensure consistent data quality. By meeting the specific client requirement of converting leads with a "Conference" lead source into accounts, contacts, and opportunities, we demonstrate the power of Apex code in extending and customizing Salesforce's capabilities.

As with any customization in Salesforce, it's important to document the solution, test it thoroughly, and keep it well-maintained. When implemented correctly, an Apex batch can be a valuable asset for any organization looking to optimize its lead conversion process.

For any queries please reach out to