Requirement
The goal is being able to convert multiple leads into Accounts, Contacts, and Opportunities programmatically. This process utilizes the Database.LeadConvert
class, which supports bulk processing to handle multiple leads efficiently.
Technical Approach
Using Apex for Bulk Lead Conversion
Salesforce provides Database.LeadConvert
in Apex which gets Lead Id as parameter and convert into Account, Contact and Opportunity.
Here is an example:
public class MassLeadConversion { public static void convertLeads(List<Id> leadIds) { if (leadIds.isEmpty()) { System.debug('No Lead is provided for conversion.'); return; } List<Database.LeadConvert> leadConversions = new List<Database.LeadConvert>(); List<Lead> leadsToConvert = [SELECT Id, IsConverted FROM Lead WHERE Id IN :leadIds]; for (Lead lead : leadsToConvert) { if (!lead.IsConverted) { Database.LeadConvert leadConvert = new Database.LeadConvert(); leadConvert.setLeadId(lead.Id); leadConvert.setDoNotCreateOpportunity(false); // Set true if no Opportunity is needed leadConvert.setConvertedStatus('Closed - Converted'); // Replace with your org's converted status // Optional: Specify existing Account or Contact // leadConvert.setAccountId(existingAccountId); // leadConvert.setContactId(existingContactId); leadConversions.add(leadConvert); } } if (!leadConversions.isEmpty()) { // Perform lead conversion List<Database.LeadConvertResult> results = Database.convertLead(leadConversions, false); for (Integer i = 0; i < results.size(); i++) { if (results[i].isSuccess()) { System.debug('Lead successfully converted: ' + leadConversions[i].getLeadId()); } else { System.debug('Lead conversion failed for Lead ID: ' + leadConversions[i].getLeadId() + '. Error: ' + results[i].getErrors()[0].getMessage()); } } } else { System.debug('All leads are already converted.'); } } }
Explanation of the Code
- Retrieve Leads: Query the leads to convert using their IDs.
- Check if the lead is already converted (
IsConverted
).
- Check if the lead is already converted (
- Setup Conversion:
- Use
Database.LeadConvert
to set the Lead ID and specify options like:setDoNotCreateOpportunity(true)
to skip opportunity creation.setConvertedStatus()
to set the desired converted status.- Optionally, link to existing accounts or contacts using
setAccountId()
orsetContactId()
.
- Use
- Perform Conversion:
- Use
Database.convertLead()
for bulk conversion. - Iterate over the results to handle success or failure for each Lead.
- Use
How can we use?
The given method can be used from Flow, LWC or Apex.
MassLeadConversion.convertLeads(leadIds);
Using Flow: We can create @invocable method and pass Lead Ids then above given code can be invoked.
Using LWC: We can create @auraEnabled method and pass List of Ids and then above given code can be further invoked.
Apex Trigger: Directly call MassLeadConversion.convertLeads(leadIds); from trigger.
Summary
- Use
Database.LeadConvert
for bulk conversion. - Handle large-scale conversions with Batch Apex.
- Ensure proper error handling and logging for failed conversions.
- Validate through test classes to cover all scenarios.
Please reach out to me at ayub.salsforce@gmail.com for any support needed.