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
Yogesh Malbhage 18Yogesh Malbhage 18 

"Too many soql rows 50001" exception apex class

We are fetching records from contact provided sufficient conditions and it is hitting the governor limit of rows returned. It required to fetch all these rows. Could you help me to handle this exception ASAP?
NagendraNagendra (Salesforce Developers) 
Hi Yogesh,

Sorry for this issue you are facing.

There is a limit to the number of records that can be retrieved by the SOQL Query which is 50000 records. Attempting to query the records having the number more than this will give the error which you are getting.To query the records more than its limit, you need to use a Batch Class which provides a fresh limit of 50k records by resetting the limits.

The exception is being thrown because the maximum number of rows you can return in SOQL calls in Apex is 50000.
 
This happens If you don't have a where clause In addition, they do not have a LIMIT clause which could prevent this exception.
 
In your VF Page, you could try to limit this to only Products in a specific Pricebook, or only products of a certain type, etc.
 
It looks like your org has an extensive catalog, so think about the use case. Even if you could display 50,000 records on a page, who is going to be able to read that?
 
If you add Where and Limit clauses to your SOQL, you'll be able to resolve the error. Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
Yogesh Malbhage 18Yogesh Malbhage 18
Hi Nagendra,

The query is already in Batch class and we are fetching these records for some calculations (So we need all of them).