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
Adriana VoyceAdriana Voyce 

We can't save this record because the “Lead: Auto Convert” process failed. Give your Salesforce admin these details. An Apex error occurred: System.LimitException: Too many query rows: 50001

Hello All, 

I have a trigger I created to auto-convert a lead. Basically, when a box is checked on a lead, it kicks off process builder to convert the lead. It used to work just fine but a couple of months ago it stopped working. Any idea why? How can I fix this? Any help would be gretly appreciated! 


Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();

List<Lead> myLeads = [Select id, name, Studio_Sync_Account_ID__c, Stripped_Website__c, website, company from Lead where id IN:LeadIds];


for (Lead l:myLeads){

}

List<Account> matchAccount = [Select id, Account_Classification__c, Full_ID__c, Stripped_Website__c, website, Name from account ];

for(Lead currentlead: myLeads){
for (Account a:matchAccount){
if (currentlead.Studio_Sync_Account_ID__c== a.Full_ID__c){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead.id);
Leadconvert.setAccountId(a.id);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
MassLeadconvert.add(Leadconvert);
Break;
}
}
}

if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
}}
ayu sharma devayu sharma dev
Hello Adriana Voyce 

Generally, this error occurs when one of you Trigger, Process or Workflow fails when auto converting the Lead. You need to troubleshoot this error. A possible solution is using Debug logs. Follow these steps:
Create a Debug Log for your user. This might help (https://help.salesforce.com/articleView?id=code_add_users_debug_log.htm&type=5).
Keep the Apex debug to the finest when creating a Debug log.
Now reproduce the Error and go back to your debug logs.
You can see some new logs there.
Open them and search for Error or Fail statements.
If everything worked fine you might find the cause of the error.
Please try this and if the problem still persists let me know.

Thanks and Regards 
Ayush Sharma
Adriana VoyceAdriana Voyce
Hi Ayush, 

I tried that but the log is not really telling me much. 

My class below and the details from the log. This works no problem in Sandbox (partial).
 
Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();

List<Lead> myLeads = [Select id, name, Studio_Sync_Account_ID__c, Stripped_Website__c, website, company from Lead where id IN:LeadIds];


for (Lead l:myLeads){

}

List<Account> matchAccount = [Select id, Account_Classification__c, Full_ID__c, Stripped_Website__c, website, Name from account ];

for(Lead currentlead: myLeads){
for (Account a:matchAccount){
if (currentlead.Studio_Sync_Account_ID__c== a.Full_ID__c){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead.id);
Leadconvert.setAccountId(a.id);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
MassLeadconvert.add(Leadconvert);
Break;
}
}
}

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

16:27:13.622 (3622512366)|FATAL_ERROR|System.LimitException: Too many query rows: 50001 Class.AutoConvertLeads.LeadAssign: line 16, column 1 16:27:13.622 (3622530819)|FATAL_ERROR|System.LimitException: Too many query rows: 50001 Class.AutoConvertLeads.LeadAssign: line 16, column 1 16:27:13.622 (3622600336)|CUMULATIVE_LIMIT_USAGE 16:27:13.622 (3622600336)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 3 out of 100 Number of query rows: 50001 out of 50000 ******* CLOSE TO LIMIT Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 1157 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10