You need to sign in to do that
Don't have an account?
![L0bster1 L0bster1](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Bulk Lead convert 200 leads at a time: Is it possible?
I want to automatically convert new leads into contacts. I have a trigger on Lead after update that will bulk convert 100 leads at a time. The problem I have is that my marketing automation tool pushes new leads into Salesforce in batches of 200 at a time. When I try to import 200 leads, the bulk convert fails due to a too many DML 151 error.
I read that the convertlead function can only handle 100 records at a time. How can I edit the code to handle 200 imports at a time? Is it possible?
Thanks in advance.
trigger AutoConvert on Lead (after update) {
for(Lead myLead: Trigger.new){ if(Trigger.new[0].isConverted == false) { Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(myLead.Id); lc.convertedStatus = 'Qualified'; //Database.ConvertLead(lc,true); lc.setDoNotCreateOpportunity(true); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); }}}
Instead of passing one lead value at a time to the convertLead method, you can pass a list of lead values. From the documentation at
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm
you can do something like
LeadConvertResult[] Database.convertLead(LeadConvert[] leadsToConvert, Boolean opt_allOrNone)
Also, you trigger is not designed for bulk operations. Use the updated trigger below and modify it as necessary.
Edit - forgot the part about max 100 records for convertLead. Updated the apex code for it
All Answers
Instead of passing one lead value at a time to the convertLead method, you can pass a list of lead values. From the documentation at
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm
you can do something like
LeadConvertResult[] Database.convertLead(LeadConvert[] leadsToConvert, Boolean opt_allOrNone)
Also, you trigger is not designed for bulk operations. Use the updated trigger below and modify it as necessary.
Edit - forgot the part about max 100 records for convertLead. Updated the apex code for it
Your code worked flawlessly. Thank you so much. I've seen a lot of posts on this issue but you were the first that I know of to find a solution.
I added some code to check if the web domain of a lead matches any accounts, and if so, associate the lead with that account at conversion. I think my SOQL statements are outside of the loops, but I'm still getting a too many SOQL statements error.
I've bolded the code that I added. Do you see what I've done wrong?
Your trigger is not so bad. It shouldnt be the real reason for the SOQL limit problem. Anyway, I noticed that you were doing a query on the lead object which I did not feel necessary as you could use trigger.new information itself for that. The updated code is below.
As for the limits part. Check the debug logs for an execution where you get this exception. This trigger should only use 1 query, and since you can call the same trigger for a max of 10 times, I am pretty sure that this trigger is not the root cause.
The reason could be because your trigger does a lead convert, which can fire account and contact triggers
Check the link
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_ignoring_operations.htm
to know if the before triggers will be called as well.
Anyway, scouring through the debug log, you can look at the apex profiling sections to see how the SOQL limit keeps increasing. You can then identify the rogue code and clean it up.
Jerun, thanks for all of your help. I tweaked the code a bit to make sure that the assignment rules would run against the lead before converting it. As part of this solution, I had to create a workflow that checked the Convert Lead checkbox when a lead is created. This step with the code below ensures that the lead is not converted before the assignment rules are run.
The code below is working great for me now.
Hi,
Can you post the final code for lead coversion to contact.
I am doing a bulk lead conversion on Lead and I notice the 'after insert' trigger for Opportunity running (which is as expected). However, the vague thing is that the trigger is running for 1 record at a time ( I am converting ~500 leads), hence I am hitting the 'TOO MANY SOQL QUERIES' exception. Has anybody noticed this? Thanks! :)
You can also try out appexchange solution https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B5LuWEAV
Simple test class as well that gets a pass (note it doesn't test for DML updates though).
Any feedback is appreciated. This class should be able to handle bulk updates, too.
And here is the test class that is 90% coverage, but could easily be 100% coverage if needed.
1) Use Salesforce Standard Report
2) Import Lead IDs using XLS file
3) Show All Leads which are not converted
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N30000000qDqqEAE
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000G0nBzUAJ