Can anyone please tell me if there is something wrong with the scrip below. I am using LIMIT 50000 but still getting this error. I will appreciate any help!


    //for mws chart
  AggregateResult[] groupedSales 
  =[SELECT CALENDAR_MONTH(Purchase_Date__c),CALENDAR_YEAR(Purchase_Date__c), SUM(Detail_Total__c)
   FROM MWSOrderItem__c 
   WHERE  Purchase_Date__c = LAST_N_DAYS:365 AND Buyer_lookup__c =:UserInfo.getUserId()
   GROUP BY CALENDAR_MONTH(Purchase_Date__c),CALENDAR_YEAR(Purchase_Date__c)
   ORDER BY CALENDAR_YEAR(Purchase_Date__c),CALENDAR_MONTH(Purchase_Date__c) ASC LIMIT 50000 ];

  List1 = new List<String>();  

 for (AggregateResult ar11 : groupedSales )  {



1 Answer 1


The Limit in your query does not apply on the Total number of rows which were accessed but on the number of AggregatedRows returned. Thus if 1 of your Aggregated Rows has touched more than 1 rows you hit the limit...

This is implied in the documentation: Working with SOQL Aggregate Functions

Queries that include aggregate functions are subject to the same governor limits as other SOQL queries for the total number of records returned. This limit includes any records included in the aggregation, not just the number of rows returned by the query. If you encounter this limit, you should add a condition to the WHERE clause to reduce the amount of records processed by the query.

but it's not 100% clear ;-)

  • I have a question, if we are making an update to this object and this query is running at the same time, will it probably hit the governor limits?
    – Carlos
    Jun 18, 2014 at 15:36
  • at the same time means in the same context, sequential or in parallel ?
    – ManSpan
    Jun 18, 2014 at 16:18
  • let me explain, this code is for a dashboard that I created for the users. We are making a mass update in this object. so it is in parallel.
    – Carlos
    Jun 18, 2014 at 16:39
  • 1
    well, if you have more than 50000 records of MWSOrderItem__c with Purchase Date within the last year for the running user, it will hit the limits regardless of any updates you are making.
    – ManSpan
    Jun 18, 2014 at 16:55
  • 1
    is @Readonly an option ?? salesforce.com/us/developer/docs/apexcode/Content/…
    – ManSpan
    Jun 18, 2014 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .