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
JAYESH RATHORJAYESH RATHOR 

How to handle Too many query rows: 50001 in apex class, In the below code

Here THIS_Month is a pick list value.
If(selectvalue =='THIS_Month')
{
List<AggregateResult> openCaseList=  [SELECT Count(id),Status from Case Where RecordType.Name !='Return Case'  AND  CreatedDate  <= THIS_MONTH Group By Status LIMIT 49999];
}
But I am getting Too many query rows: 50001 
please give me any solution on it.
JAYESH RATHORJAYESH RATHOR
Same soql code has been working on developer console but in apex class it's not working.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Jayesh,
  • The exception is being thrown because the maximum number of rows you can return in SOQL calls in Apex is 50000.
  • You will need to use Batch Apex, in which the 50k limit counts per batch execution
  • These limits count for each Apex transaction. For Batch Apex, these limits are reset for each execution of a batch of records in the execute method
  • Can you limit the results by adding either some more where criteria or using a LIMIT 50000 statement in the soql.

Please refer the below link for further reference. I hope it will be helpful.

Please mark it as best Answer if the information is informative.

Best Regards
Rahul Kumar


 
JAYESH RATHORJAYESH RATHOR
Thanks for your reply!@rahul