This blog explains how to speed up and optimize the Salesforce lead conversion process using a combination of Screen Flow and Apex. The goal is to allow users to select multiple leads from the list view and convert them all at once, reducing manual work and saving time. The approach focuses on patterns and reusability, not internal tools or proprietary details.
Introduction
In Salesforce, lead conversion is the process of changing a lead into an account, a contact, and, optionally, an opportunity. By default, Salesforce lets you convert leads one at a time, which can be time-consuming when dealing with multiple leads. In this blog, we’ll explore how you can make this process faster by using Flow and Apex. We’ll show how users can select multiple leads in a list view and convert all selected leads with a single click, making lead management much more efficient.
With this use case, we aim to:
- Enable users to select specific Leads for conversion
- Passing the selected Lead IDS to an Apex class that uses Database.LeadConvert will convert them into corresponding Account and Contact records.
- Send an email notification summarising the outcome of the conversion process.
Approach
The solution combines Salesforce Flow for the user interface with Apex for backend logic.
- Flow captures the leads the user selects.
- Apex performs the actual conversions.
- Batch Apex ensures the process can handle multiple records safely.
- A custom button on the Lead list view triggers the Flow.
Walkthrough
1. Create a Screen Flow
Navigate to Setup, search for ‘Flows’, and create a new Screen Flow.
2. Create a Flow Variable to Store Selected Record IDs
In the flow, define a variable named ids to hold the list of record IDS the user selects from the list view. This variable will pass the selected Lead records for further processing.
3. Count Selected Lead IDs
Next, use an Assignment element to initialise a Number variable to store the count of selected Lead IDs.
4. Limit Conversion Volume with Decision Element
Add a Decision element to verify whether the number of selected Lead IDs is 30 or fewer. This ensures the process handles a manageable number of conversions at a time.
5. Call Apex Action for Lead Conversion (If 30 or Fewer Leads Selected)
If the number of selected leads is 30 or fewer, proceed to call the Apex Action.
Pass the IDs collection variable (containing the selected Lead IDs) as the input parameter to the Apex method.
6. Adding Display Text Below Apex Action
Next, place a screen element below the Apex action and add a display text element.
7. Limiting Lead Selection to 30
If the outcome exceeds 30, add another screen element with display text indicating that only up to 30 leads can be selected.
8. Apex Class for Flow Invocation
LeadConstructor-
In the LeadConstructor class, we receive a list of Lead IDs from a Flow. These IDs are passed to a batch class via Database.executeBatch, which triggers the leadConversionBatchApex class.
CLASS LeadConstructor
// This class is exposed to Flow using an Invocable Method
METHOD callLeadBatch(leadsList) [Invocable Method]
EXTRACT list of Lead Ids from the incoming data
RUN the LeadConversionBatch process with those Lead Ids in small chunks (batch size = 2)
END METHOD
// Helper inner class to define the structure of input data
CLASS LeadData
VARIABLE leadIds (list of Lead record IDs)
END CLASS
END CLASS
leadConversionBatchApex-
In the leadConversionBatchApex class, the constructor receives the list of Lead IDs passed from the LeadConstructor class. These IDs are then used in the start method to query only the unconverted Lead records.
In the execute method, we retrieve the converted Lead status and use it to create a list of Databases.LeadConvert objects.
CLASS LeadConversionBatch IMPLEMENTS Batchable, Stateful
VARIABLE leadIds
CONSTRUCTOR(leadIds)
STORE the received Lead Ids for processing
END CONSTRUCTOR
METHOD start()
QUERY all unconverted leads that match the given Ids
END METHOD
METHOD execute(batchContext, leadsToConvert)
FIND the standard “Converted” status value for leads
INITIALIZE an empty list of lead conversion requests
FOR each lead in leadsToConvert
PREPARE a conversion request:
– Set the lead Id
– Assign the converted status
ADD to the list of conversions
END FOR
IF the list of conversion requests is not empty
PERFORM bulk lead conversion
END IF
END METHOD
METHOD finish()
// Optional: perform any cleanup or logging after batch completion
END METHOD
END CLASS
9. Creating a Button to Add Flow to Lead List View
Next, navigate to the Lead object manager and create a new button to add the flow to the Lead list view.
The flow URL is/flow/Convert_leads , and to redirect to the Open Leads tab, we must add retURL=lightning/o/Lead/list?filterName=AllOpenLeads.
10. Saving and Adding the Button to the Page Layout
Save the button and place it in the page layout. You can now use this button to convert leads directly from the list view in the Leads tab.
Key Learnings
- Flow + Apex = Efficiency: Flow handles the UI, Apex manages bulk logic.
- Governor Limits: Always restrict the number of records processed.
- Batch Apex helps handle larger data volumes safely.
- Keep code generic and reusable, not tied to specific fields or org setups.
- Invocable Apex allows smooth integration with Flows.
Results
- Reduces manual effort by automating multiple lead conversions.
- Improves overall speed and accuracy in lead management.
- Enhances sales productivity by letting users focus on leads rather than processes.
- Provides a scalable framework for other bulk operations.
Conclusion
By combining Salesforce Screen Flow with Apex and Batch Apex, we created a simple yet powerful automation for bulk lead conversion. This solution improves efficiency, minimises manual work, and ensures data accuracy — all while using Salesforce’s standard capabilities.
Author: Bharath Reddy
