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
NLTNLT 

Error in test class 'System.LimitException: Too many query rows: 50001'

Hi, I am getting below error when running the test class and test method is failing. Please help to resolve it.

Error MessageSystem.LimitException: Too many query rows: 50001

Stack Trace(System Code)
Class.OpportunityTriggerHandler.handleAfterUpdate: line 495, column 1
Trigger.OpportunityTrigger: line 18, column 1
SwethaSwetha (Salesforce Developers) 
HI Lakshman,
This is one of the governor limits of the platform.

In a single apex context your queries can return a max of 50000 records else you will hit the limit as the error suggests.

Consider first adding sufficient filter to your query, if you cannot then you have to move your logic into a batch apex that can process records in chunks and provides ability to query more than 50K rows via query locator in the start method.

See similar posts from past to get started:
https://salesforce.stackexchange.com/questions/105609/system-limitexception-too-many-query-rows-50001-even-though-soql-has-limit-10
https://www.levelupsalesforce.com/too-many-query-rows-50001-in-salesforce

If this information helps, please mark the answer as best. Thank you