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
dnvansantdnvansant 

Question about "System.LimitException: Too many query rows: 50001"

I'm getting this error message:

 

"System.LimitException: Too many query rows: 50001", Failure Stack Trace: "(System Code) Trigger.TimeSeries: line 6, column 1"

 

Line 6 on Trigger.TimeSeries is not within a loop and queries an obect with only 10,000 records. So how am I exceeding 50,000 rows?

 

Is it possible that 50,000 refers to the sum of several different queries, but that it's just the TimeSeries query that pushes the total over 50,000?

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

1. Batch update of 5 records will execute the trigger once. All 5 Account records are available in a list called Trigger.new.  Can you take advantage of the knowledge of the n Account.id fields known to the trigger to limit the SOQL query?

 

2. If the SOQL query really has > 50,000 rows returned, then you can use Batch Apex called from your trigger to do the work. This is described in detail in the apex developer's guide. You can launch it from an @future method so as to not tie up the user's response time

All Answers

kiranmutturukiranmutturu

Is it possible that 50,000 refers to the sum of several different queries  -->  this is not true..

 

 

some where in the running instance you are getting the 50,000 records in a query .... check the size of the records at line 6 once ....

Hengky IlawanHengky Ilawan

Hi,

 

You may have other queries within the same execution and the total sum of records exceeded 50K.

 

Regards,

Hengky

dnvansantdnvansant

Below is the first 7 lines of the TimeSeries Trigger, which includes the only query in the trigger.

 

So, if I batch update 5 Accounts, will this trigger run 5 seperate times? And if the query returns 10000+ records, would this result in cumulatively more than 50,000 records?

 

Is this what you mean by referring to a single "execution"?

 

Thank you for your help with this.

 

-Derek

 

trigger TimeSeries on Account (after Update, after Insert) {

List<Time_Series_Reporting__c> ac = new List<Time_Series_Reporting__c>();
List<Time_Series_Reporting__c> acnew = new List<Time_Series_Reporting__c>();
Map<String,Id> UniqueIDtoTSID = new Map<String,Id>();
for (Time_Series_Reporting__c ts :[SELECT ID, UniqueID__c from Time_Series_Reporting__c where name != 'sdad']){UniqueIDtoTSID.put(ts.UniqueID__c, ts.Id);}

 

crop1645crop1645

1. Batch update of 5 records will execute the trigger once. All 5 Account records are available in a list called Trigger.new.  Can you take advantage of the knowledge of the n Account.id fields known to the trigger to limit the SOQL query?

 

2. If the SOQL query really has > 50,000 rows returned, then you can use Batch Apex called from your trigger to do the work. This is described in detail in the apex developer's guide. You can launch it from an @future method so as to not tie up the user's response time

This was selected as the best answer
dnvansantdnvansant

Thank you for everyone's help. I still don't thoroughly understand why the query was returning 50000+ rows, but I restuctured the query to pull fewer rows and it's working now. Code is below for anyone that's interested.

 

-Derek

 

trigger TimeSeries on Account (after Update, after Insert) {

List<Time_Series_Reporting__c> ac = new List<Time_Series_Reporting__c>();
List<Time_Series_Reporting__c> acnew = new List<Time_Series_Reporting__c>();
set<String> UniqueIDs = new set<String>();

for(Account myAccount: Trigger.new){
            UniqueIDs.add(myAccount.TS_Unique_ID_Daily__c);
            UniqueIDs.add(myAccount.TS_Unique_ID_Weekly__c);
            UniqueIDs.add(myAccount.TS_Unique_ID_Monthly__c);
    }

Map<String,Id> UniqueIDtoTSID = new Map<String,Id>();
for (Time_Series_Reporting__c ts :[SELECT ID, UniqueID__c from Time_Series_Reporting__c where UniqueID__c in :UniqueIDs ])
{UniqueIDtoTSID.put(ts.UniqueID__c, ts.Id);}