function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VPM 1VPM 1 

lead conversion using batch apex

Has anyone worked on an Batch Class which converts leads. If yes please send me sample code
ANUTEJANUTEJ (Salesforce Developers) 
Hi VPM,

>> https://salesforce.stackexchange.com/questions/38/mass-lead-conversion-programmatically-or-using-data-loader

The above link has details on how to mass convert leads in apex code, please try checking it.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
jayashree bhosalejayashree bhosale
Hi VPM,
I think my code may help you. Do modifications as per your requirement.
  • Bulk Lead conversion with batchable class
//------------------------------------------------------------------------------------------------------------------------------------------------

public class Lead_Conversion_Batch implements Database.Batchable<sObject>{
    
    String query = 'SELECT id,Name,FirstName,LastName,Company,LeadSource,Rating,Title,IsConverted from Lead where IsConverted = false limit 1';
    public Database.QueryLocator start(Database.BatchableContext BC){  
        System.debug('Query :: '+query);
        return Database.getQueryLocator(query);
    }
    
    public void execute(Database.BatchableContext BC,List<Lead> scope){
        //System.debug('Inside Execute Batch');
        list<Lead> LeadsToConvert = new list<Lead>(); 
        for(sObject lead : scope){
            LeadsToConvert.add((Lead)lead);
        }
        System.debug(LeadsToConvert);
        String convertedStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1].MasterLabel;
        list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
        for(Lead myLead : LeadsToConvert){
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(myLead.Id);
            lc.setConvertedStatus(convertedStatus);            
            //Database.ConvertLead(lc,true);
            lc.setDoNotCreateOpportunity(true);
            leadConverts.add(lc);
        }
        
        if(!leadConverts.isEmpty()){
            for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
                list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
                Integer startIndex = i*100;
                Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
                for(Integer j=startIndex;j<endIndex;j++){
                    tempList.add(leadConverts[j]);
                }
                Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
                for(Database.LeadConvertResult lcr : lcrList)
                    System.assert(lcr.isSuccess());
            }
        }
        System.debug('Lead Conversion succeed');
        
    }
    
    public void finish(Database.BatchableContext BC){
        System.debug('In Finish');
    }
}

//----------Anonymous Block Execution code----
Lead_Conversion_Batch lcb = new Lead_Conversion_Batch();
// Invoke the batch job.
ID batchprocessid = Database.executeBatch(lcb);
System.debug('Returned batch process ID: ' + batchProcessId);

//---------------------------------------------------------------------------
Thanks.
TobyKutlerTobyKutler
This would be a helpful resource for you: 

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_dml_convertLead.htm
kelly sniderkelly snider
Never work on it before. But maybe the Seint Official systems may know about it as they had buid a website.