+ Start a Discussion
Jack Davis 3Jack Davis 3 

Error System.LimitException: Too many query rows: 50001

Hi all,

A developer had written an Apex Trigger for us years ago and we're now getting an error. We don't have a developer at the moment due to him having covid19, I was wondering whether anyone here can assist. This is stopping us creating tasks.

This is the error we're getting:
Apex trigger TaskTrigger caused an unexpected exception, contact your administrator: TaskTrigger: System.LimitException: Too many query rows: 50001

Here's the code:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
MODIFICATION HISTORY

RG  ray@forceclarity.com (email me!)

07/09/2012  RG  created
23/10/2012  RG  enabled OnBeforeInsert for UpdateServiceNameOnOutbounds() 
06/11/2012  RG  enabled AfterInsert and AfterUpdate for calculation of Total Outbound Calls on parent Account
10/12/2014  RG  added Trigger.newMap to before insert and before update  
  
*/

trigger TaskTrigger on Task (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    
    Task_TriggerHandler handler = new Task_TriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if (Task_TriggerHandler.isExecuteTrigger) {
      
        if(Trigger.isInsert && Trigger.isBefore){
            handler.OnBeforeInsert(Trigger.new, Trigger.newMap);
        } 
        else if(Trigger.isInsert && Trigger.isAfter){
            handler.OnAfterInsert(Trigger.new, Trigger.newMap);
            //TaskTriggerHandler.OnAfterInsertAsync(Trigger.newMap.keySet());
        }
    
        else if(Trigger.isUpdate && Trigger.isBefore){
            handler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
        }
       
        else if(Trigger.isUpdate && Trigger.isAfter){
            handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap);
            //TaskTriggerHandler.OnAfterUpdateAsync(Trigger.newMap.keySet());
        }        
    }
    

}

 
ShirishaShirisha (Salesforce Developers) 
Hi Jack,

Greetings!

This error occurs when you are trying to return more than 50000 records from the SOQL query.In order to resolve the issue either you will have to limit the number of records returned from the SOQL or you will have to process them in the chunks.

Also,I can see that you do not have any SOQL queries in the Trigger and it is calling the trigger handler named as "Task_TriggerHandler".So,can you please check for the SOQL query in the above Apex class and limit as below:

Ex:Select ......... from ..... Limit 50,000.

 which will resolve the issue.

Reference:https://salesforce.stackexchange.com/questions/105609/system-limitexception-too-many-query-rows-50001-even-though-soql-has-limit-10

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Rahul Kumar DeyRahul Kumar Dey
@Jack, We have limit to get 50000 records per transation by SOQL. The solution is, you can divide this into batch jobs with 200 records per batch. 
Jack Davis 3Jack Davis 3

Hi @Rahul. 

I'm not a developer. Ours is currently unavailable due to contracting the virus. Would you be able to assist at all? 

The reason why I posted here is because I'm unable to do it myself. 

Any help is greatly appreciated 

ShirishaShirisha (Salesforce Developers) 
Hi Jack,

Greetings!

Can you please open the apex class "Task_TriggerHandler " as below:

>>Go to Setup>Search for Apex class in the Quick FindBox>Search for the class "Task_TriggerHandler " in the list.

>>Check for the SOQL queries in the above class and add limit 50000 at the end of the query.

If it is production then,you will not be able to edit the class directly.So,you will have to do it in the sandbox and then deploy it into the Production.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Jack Davis 3Jack Davis 3
Hi Shirisha

Where can I find out where the SOQL queries are?
 
ShirishaShirisha (Salesforce Developers) 
Hi Jack,

Once,you are on the Class page look for the SOQL query which starts with 

"Select" and try by running that SOQL query in developer console to see,how many records that were returned if you have more than one.

Please check the reference Document for more information:

https://help.salesforce.com/articleView?id=code_dev_console_tab_query_editor_soql_sosl.htm&r=https%3A%2F%2Fwww.google.com%2F&type=5

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Jack Davis 3Jack Davis 3
Hi Shirisha 

The code has a lot of Select statements and I'm not sure which one the code relates to also getting an Unknown error in parsing query.

Regards

Jack