Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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!

code:

    //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 )  {

   List1.add(String.valueof(ar11.get('expr0')+'/'+String.valueof(ar11.get('expr1'))));
   List1.add(String.valueof(ar11.get('expr2')));

 } 
share|improve this question

1 Answer 1

up vote 11 down vote accepted

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 ;-)

share|improve this answer
    
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 at 15:36
    
at the same time means in the same context, sequential or in parallel ? –  ManSpan Jun 18 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 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 at 16:55
1  
is @Readonly an option ?? salesforce.com/us/developer/docs/apexcode/Content/… –  ManSpan Jun 18 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.