You need to sign in to do that
Don't have an account?
Rajbharath
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.
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); } } } }
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. For more information please check with below link from the forums community.
- https://salesforce.stackexchange.com/questions/38/mass-lead-conversion-programmatically-or-using-data-loader
Hope this helps.Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra