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
Sudeep SinghSudeep Singh 

Lead to contract conversion using batch

Hi,

Currently am converting Lead to Contract, while converting it creates account, Memberplan, Address record also. 

Now I need to do it in Batch so that it will convert all the leads at a single time. Batch should run according the condition of class am using currently.

How to achieve this ?

Thanks

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Can you share the current apex class which you are using to convert lead to Contract. 

Thanks,
 
Sudeep SinghSudeep Singh
public class AutoConvertLead {
    public static String errmsg = '';
@InvocableMethod
public static void assignLeads(List<Id> LeadIds) { 
    try{ 
        Map<String,Date> NameDOBMap = new Map<String,Date>();
        List<String> leadNames = new List<String>();
        List<Date> leadDOBs = new List<Date>();
        List<Database.LeadConvert> massLeadConvert = new List<Database.LeadConvert>();
        List<Account> accountsToUpdate = new List<Account>();
        List<Account> accountsToInsert = new List<Account>();
        List<Contract> contractsToInsert = new List<Contract>();
        List<Addresses__c> addressesToInsert = new List<Addresses__c>();
        List<MemberPlan> memberplanToInsert = new List<MemberPlan>();
        Map<String,Account> nameDOBToAccountMap = new Map<String,Account>();
        Map<String,Account> newCreatedAccounts = new Map<String,Account>();
        Map<ID,Id> leadProductMap = new Map<Id,Id>();
        Map<ID,Id> leadHealthcareFacilityMap = new Map<ID, Id>();
        
        Id personAccountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        LeadStatus cLeadStatus = [SELECT Id,MasterLabel FROM LeadStatus WHERE isConverted = true LIMIT 1];  
        
       // List<Lead> leads = [SELECT Id,Name,LastName,FirstName,Email,Date_Of_Birth__c,Service__c,Ethnicity__c, Race__c,Gender__c FROM Lead WHERE Id IN :LeadIds];
       Map<Id,Lead> leadmap= new Map<Id,Lead>([SELECT Id,Name,LastName,FirstName,Email,Signature__c,Signature_Date_Time__c,Address_Line_2__c,Date_Of_Birth__c,Service__c,Depot_Name__c,Ethnicity__c,MobilePhone,Phone, Race__c,Gender__c,City,Country,State,Street,PostalCode,Address_Type__c,Group_ID__c,Insurance_Name__c,Is_Policy_Holder__c,Policy_Holder_First_Name__c,Policy_Holder_Last_Name__c,Policy_ID__c,Insurance__c FROM Lead WHERE Id IN :LeadIds]);
       // for(Lead lead: leads) {
        for(Lead lead: leadmap.values()) {
            leadNames.add(String.valueOf(lead.Name));
            leadDOBs.add(lead.Date_Of_Birth__c);
            NameDOBMap.put(lead.Name, lead.Date_Of_Birth__c);
            leadProductMap.put(lead.Id,lead.Service__c);
            leadHealthcareFacilityMap.put(lead.Id,lead.Depot_Name__c);
        }
        
        Map<Id, Account> existingAccounts = new Map<Id, Account>([SELECT Id, PersonBirthdate, Name, PersonGender FROM Account WHERE Name IN :NameDOBMap.KeySet() AND PersonBirthdate IN :NameDOBMap.values()]);
        
        for(account acc: existingAccounts.values()){
            
        }
        //System.debug('Check 1'+existingAccounts);
        for(Account accc: existingAccounts.values()){
            nameDOBToAccountMap.put(accc.Name+accc.PersonBirthdate, accc);
        }
        
       // for(Lead ld : leads){
        for(Lead ld: leadmap.values()) {
            if(!nameDOBToAccountMap.containsKey(ld.Name+ld.Date_Of_Birth__c)){
                Account existingAccount = new Account();
                existingAccount.LastName = ld.LastName;
                existingAccount.FirstName = ld.FirstName;
                existingAccount.PersonEmail = ld.Email;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.RecordTypeId =  personAccountRecordTypeId;
                existingAccount.Ethnicity__pc = ld.Ethnicity__c;
                existingAccount.Race__pc = ld.Race__c;
                existingAccount.PersonGender = ld.Gender__c;
                existingAccount.PersonBirthdate = ld.Date_Of_Birth__c;
                existingAccount.PersonMobilePhone = ld.MobilePhone;
                existingAccount.Phone = ld.Phone;
                existingAccount.Address_Line_2__c = ld.Address_Line_2__c;
                accountsToInsert.add(existingAccount);
            }  
        }
        
        if(accountsToInsert.size()>0){
            insert accountsToInsert;
        }
        //system.debug('accountsToInsert'+accountsToInsert);
        
        for(account acc: accountsToInsert){
            newCreatedAccounts.put(acc.FirstName+acc.LastName, acc);
        }
        //system.debug('newCreatedAccounts'+newCreatedAccounts);
        //for(Lead lead : leads){
            for(Lead lead : leadmap.values()){
            //System.debug('Check 2');
            Database.LeadConvert LeadConvert = new Database.LeadConvert();
            LeadConvert.setLeadId(lead.Id);
            LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);
            LeadConvert.setDoNotCreateOpportunity(True);
            
            if(lead.Name != null && lead.Date_Of_Birth__c != null){                    
                if(nameDOBToAccountMap.containsKey(lead.Name+lead.Date_Of_Birth__c) && nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c)!=Null){
                    //system.debug('existingAccount==>'+nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c));
                    Account existingAccount = new Account();
                    existingAccount.Id = nameDOBToAccountMap.get(lead.Name+lead.Date_Of_Birth__c).id;
                    //system.debug('existingAccount'+existingAccount);
                    existingAccount.Ethnicity__pc = lead.Ethnicity__c;
                    existingAccount.Race__pc = lead.Race__c;
                    existingAccount.PersonGender = lead.Gender__c;
                    existingAccount.PersonHomePhone = lead.MobilePhone;
                    existingAccount.PersonEmail = lead.Email;
                    existingAccount.Phone = lead.Phone;
                    existingAccount.PersonMobilePhone = lead.MobilePhone;
                    existingAccount.Address_Line_2__c = lead.Address_Line_2__c;
                    existingAccount.BillingCountry = lead.Country;
                    existingAccount.BillingPostalCode = lead.PostalCode;
                    existingAccount.BillingState = lead.State;
                    existingAccount.BillingStreet = lead.Street;
                    existingAccount.BillingCity = lead.City;
                    accountsToUpdate.add(existingAccount);
                    LeadConvert.setAccountId(existingAccount.Id);
                }
                else{
                    //system.debug('lead.Name+lead.Date_Of_Birth__c'+lead.FirstName+lead.LastName);
                    if(newCreatedAccounts.containskey(lead.FirstName+lead.LastName) && newCreatedAccounts.get(lead.FirstName+lead.LastName)!= Null){
                        LeadConvert.setAccountId(newCreatedAccounts.get(lead.FirstName+lead.LastName).Id);
                    }  
                }
                //System.debug('LeadConvert.getAccountId()'+LeadConvert.getAccountId()); 
                massLeadConvert.add(LeadConvert);
                //system.debug('massLeadConvert'+massLeadConvert);
            }
        }
        
        if(!accountsToUpdate.isEmpty()){
            //system.debug('accountsToUpdate'+accountsToUpdate);
            update accountsToUpdate;
            //system.debug('accountsToUpdate'+accountsToUpdate);
            
        }
        
        if(!massLeadConvert.isEmpty()){
            //system.debug('massLeadConvert'+massLeadConvert);
            List<Database.LeadConvertResult> lcr = Database.convertLead(massLeadConvert);
            for(Database.LeadConvertResult leadConvertResult : lcr) {
                //system.debug('leadConvertResult'+leadConvertResult);
                //system.debug('accountid'+existingAccounts.containskey(leadConvertResult.accountid));
                if(leadConvertResult.isSuccess()  ) {
                    Contract contract = new Contract();
                    contract.Status = 'Draft';
                    contract.AccountId = leadConvertResult.getAccountId();
                    contract.StartDate = system.today();
                    contract.ContractTerm = 12;
                    if(leadProductMap.containskey(leadConvertResult.getLeadId()) && leadProductMap.get(leadConvertResult.getLeadId())!=Null){
                        contract.Service__c = leadProductMap.get(leadConvertResult.getLeadId());
                    }
                    if(leadHealthcareFacilityMap.containskey(leadConvertResult.getLeadId()) && leadHealthcareFacilityMap.get(leadConvertResult.getLeadId())!=Null ){
                        contract.Depot_Name__c = leadHealthcareFacilityMap.get(leadConvertResult.getLeadId());
                    }
                    if(leadmap.containskey(leadConvertResult.getLeadId())  ) {
                        contract.BillingCity= leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.BillingState= leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.BillingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.BillingStreet=leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.BillingPostalCode=leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        contract.ShippingCity = leadmap.get(leadConvertResult.getLeadId()).City;
                        contract.ShippingState = leadmap.get(leadConvertResult.getLeadId()).State;
                        contract.ShippingCountry = leadmap.get(leadConvertResult.getLeadId()).Country;
                        contract.ShippingStreet = leadmap.get(leadConvertResult.getLeadId()).Street;
                        contract.ShippingPostalCode = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        contract.Signature__c = leadmap.get(leadConvertResult.getLeadId()).Signature__c;
                        contract.Signature_Date_Time__c = leadmap.get(leadConvertResult.getLeadId()).Signature_Date_Time__c;
                        contract.Address_Line_2__c = leadmap.get(leadConvertResult.getLeadId()).Address_Line_2__c; 
                        
                    }
                        Addresses__c Address = new Addresses__c();
                        Address.Account__c = leadConvertResult.getAccountId();
                        Address.Address_Type__c = leadmap.get(leadConvertResult.getLeadId()).Address_Type__c;
                        Address.Name = leadmap.get(leadConvertResult.getLeadId()).Street;
                        //Address.Street__c = leadmap.get(leadConvertResult.getLeadId()).Street;
                        Address.City__c = leadmap.get(leadConvertResult.getLeadId()).City;
                        Address.State__c = leadmap.get(leadConvertResult.getLeadId()).State;
                        Address.Zip_Code__c = leadmap.get(leadConvertResult.getLeadId()).PostalCode;
                        Address.Country__c = leadmap.get(leadConvertResult.getLeadId()).Country;
                        //Address.Landmark__c = leadmap.get(leadConvertResult.getLeadId()).City;
                    
                        MemberPlan member = new MemberPlan();
                        member.MemberId = leadConvertResult.getAccountId();
                        member.Is_Policy_Holder__c = leadmap.get(leadConvertResult.getLeadId()).Is_Policy_Holder__c;
                        member.Name = leadmap.get(leadConvertResult.getLeadId()).Insurance_Name__c;
                        member.Insurance_Options__c = leadmap.get(leadConvertResult.getLeadId()).Insurance__c;
                        member.MemberNumber = leadmap.get(leadConvertResult.getLeadId()).Policy_ID__c;
                        member.GroupNumber = leadmap.get(leadConvertResult.getLeadId()).Group_ID__c;
                        member.Policy_Holder_First_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_First_Name__c;
                        member.Policy_Holder_Last_Name__c = leadmap.get(leadConvertResult.getLeadId()).Policy_Holder_Last_Name__c;
                    contractsToInsert.add(contract);
                    memberplanToInsert.add(member);
                    addressesToInsert.add(Address);
                }
            }
        }        
        if(!contractsToInsert.isEmpty()){
            insert contractsToInsert;
        }
        if(!memberplanToInsert.isEmpty()){
            insert memberplanToInsert;
        }
        if(!addressesToInsert.isEmpty()){
            insert addressesToInsert;
        }
    }catch(Exception e){
        errmsg = e.getMessage();
        //System.debug('Error: '+e.getMessage()+ 'Line Number'+e.getLineNumber());
        Batch_Message__c batch = new Batch_Message__c();
        batch.Conversion__c = 'Conversion Failed';
        batch.Conversion_Description__c = errmsg;
        insert batch;
    }
    if(errmsg == ''){
        Batch_Message__c batch = new Batch_Message__c();
        batch.Conversion__c ='Conversion Success';
        insert batch;
    }
}   
}

This is the current class that is converting lead to contract. Now I need to automate this thing as well that how many leads will be coming then it should pick all lead on that date and convert it as per this class. 

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
HI Sudeep,

Do you mean that all the lead records created on that day should be converted.

Thanks.,
 
Sudeep SinghSudeep Singh
Hi Sai
Yes exactly the lead record created that day it should get converted as per above logic.

Suppose 500 leads are created on 28/02/2023 then it should pick all the 500 records and convert it as per scheduled timing

Thanks
Sudeep SinghSudeep Singh
Hi Sai is there solution ?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

You can write a seperate batch class and add the lead ids and call this methos directly as below.
 
public class AutoconvertBatch implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('select id,Name,CreatedDate from Lead where Createddate=today');
   }

   public void execute(Database.BatchableContext BC, List<Lead> scope){
       List<Id> leadids= new List<Id>();
      for(Lead s : scope)
      {
  leadids.add(s.id);
      }
       
       if(leadids.size()>0){
           
           AutoConvertLead.assignLeads(LeadIds);
       }
   }

   public void finish(Database.BatchableContext BC){

   }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Sudeep SinghSudeep Singh
No its throwing error, while executing. It is getting unsuccesful due to System.DMLException.

Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
hi Sudeep,

Can you confirm on which line you are getting issue.

Thanks
 
Sudeep SinghSudeep Singh
Not on the particular line. Batch class is not giving any error while saving but while executing it is not converting those leads. Thanks
Sudeep SinghSudeep Singh
There are some future method for other functionalities and those objects are also being used in this class. If am deactivating those flows then the batch is running perfectly. Else throwing error.

Thnks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

Do you mean because of those future methods the batch class is failing?

Thanks,
 
Prateek Prasoon 25Prateek Prasoon 25
To achieve this, you can create a batch Apex class that queries for all the Leads that meet your condition and then converts them to Contracts. Here's an example code snippet that you can modify to suit your needs:
global class LeadConversionBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // Query for all the Leads that meet your condition
        String query = 'SELECT Id FROM Lead WHERE YourConditionHere';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        // Convert each Lead in the batch to Contract
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
        for (sObject record : scope) {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(record.Id);
            lc.setDoNotCreateOpportunity(true);
            leadConverts.add(lc);
        }
        Database.convertLead(leadConverts, true);
    }
    
    global void finish(Database.BatchableContext BC) {
        // Execute any post-processing logic here
    }
}

You can then schedule the batch Apex class to run at the desired time interval using the System.scheduleBatch() method. Here's an example
String jobName = 'LeadConversionBatch';
String cronExpression = '0 0 0 ? * *'; // Runs every day at midnight
LeadConversionBatch batch = new LeadConversionBatch();
System.scheduleBatch(batch, jobName, 1, 10);

If you find this answer helpful, Please mark it as the best answer.
Sudeep SinghSudeep Singh
Yes Sai praveen due to future method batch class is failing
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sudeep,

You can add the below condition in the place you are calling future class so it wil not be calling when batch is executing.
 
If(System.IsBatch() == false )

​​​​​​Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,