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
Vital AzaVital Aza 

agregateresult and gov limits

Consider the following transaction:

AggregateResult[] x = 
[SELECT SellToCust__r.Id,  SUM(InvAmount__c) 
FROM Invoice__c 
WHERE InvDate__c >= 2023-01-01 AND InvDate__c <= 2023-08-31 
GROUP BY SellToCust__r.Id
HAVING SUM(InvAmount__c) >= 2000];

where SellToCust__c is the field that relates an invoice__c (child) to the corresponding Account.

If I try this in an anon block of code I run against the 50,000 row limit. Batch Apex cannot handle results from such SOQL queries. If I try, for example, to simplify the query and then do the grouping etc. myself in Batchable execute() and finish() methods, I run into the heap size issue.

How would you approach this issue?
I am a novice developer and I find that I spend as much time writing logic as looking for ways to work around governor limits. Anyone else?
Best Answer chosen by Vital Aza
Julien SalensonJulien Salenson
Hi vital,

You should take a lookt at Batch Apex with Iterable.This approach doesn't load all the records into memory at once, which helps to avoid heap size issues.

Look at this thread which is somewhat similar Iterable Batch giving error -- Too many query rows: 50001
https://developer.salesforce.com/forums/?id=9062I000000g4y4QAA


Thanks for liking my comment or mark as best answer if it helps you.
Julien