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
RajbharathRajbharath 

How to bulk convert Leads through anonymous window

Hello all,

I wrote the below trigger code for converting bulk leads into accounts through a trigger handler. I have a unique id field on both Account and Lead objects, which is a combination of Site ID+Email. If there is a existing account with the same UniqueID, just copy the data into empty fields. If no existing account is found, create a new one. The below Trigger code works totally fine,but I need to do the same through anonymous window. Suppose say there are 100 Leads in my org. I need to convert them all into accounts. If found existing accounts, just copy the data or if not, create a new account.
 
public class LeadInsertTriggerHandler {

	public static void AfterInsert(List<Lead> lstLeads)
	{
		LeadStatus convertStatus = [select MasterLabel from LeadStatus where IsConverted = true limit 1];
        List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
       
		
		//Code for bulkifying
		Map<String, List<Account>> mapAccounts = new Map<String, List<Account>>();
		Set<String> setEmails = new Set<String>();
		for(Lead objLead : lstLeads) {
			setEmails.add(objLead.Email_and_SiteID__c);
            system.debug('Set Emails' + setEmails);
		}
		
		if(!setEmails.isEmpty()) {
			for(Account objAccount : [select id, Email_and_SiteID__c from account where Email_and_SiteID__c In: setEmails]) {
				if(mapAccounts.containsKey(objAccount.Email_and_SiteID__c)) {
					 mapAccounts.get(objAccount.Email_and_SiteID__c).add(objAccount);
                  }
				else {
					mapAccounts.put(objAccount.Email_and_SiteID__c, new List<Account>{objAccount});
                  }
			}
		}
		
		for (Lead lead: lstLeads) {
			if (!lead.isConverted) {
				Database.LeadConvert lc = new Database.LeadConvert();
                lc.setLeadId(lead.Id);
				lc.setConvertedStatus(convertStatus.MasterLabel);
				lc.setDoNotCreateOpportunity(TRUE);

				//bulkifying here
				if(mapAccounts.size()>0 && mapAccounts.containsKey(lead.Email_and_SiteID__c))
				{
					lc.setAccountId(mapAccounts.get(lead.Email_and_SiteID__c)[0].id);
				}
                 leadConverts.add(lc);
                 
		}

		if (!leadConverts.isEmpty()) {
			List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
		}
    }
    }
}

 
NagendraNagendra (Salesforce Developers) 
Hi Bharath,

At an absolute minimum, the following code will bulk convert leads, 1000 at a time in the anonymous window.

Please tweak the code as per your requirement.
Database.LeadConvert[] converts = new Database.LeadConvert[0];
LeadStatus convertedStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
for(Lead record: [SELECT Id FROM Lead WHERE IsConverted = false LIMIT 1000]) { 
    Database.LeadConvert convert = new Database.LeadConvert();
    convert.setLeadId(record.Id); 
    convert.setConvertedStatus(convertedStatus.MasterLabel);
    converts.add(convert);
}
Database.convertLead(converts);
For more information please check with below link from the forums community. Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
RajbharathRajbharath
@Nagendra I have already seen that post. That is to convert a new Lead into account right? My requirement is a little bit different