So I have a query:

AggregateResult[] results = [select max(Cash_Rewards_Number_Field__c) MaxName from Team__c];

This started causing 50001 too many rows returned errors in my trigger. When checking the debug logs, it seems as though it counted the total number of records in the team object against the query rows returned limit.

I also Tried:

List<Team__c> highestCashRewardTeam = [select Cash_Rewards_Number_Field__c from Team__c ORDER BY Cash_Rewards_Number_Field__c DESC LIMIT 1];

but the same problem arrises. Is there no way to get the max value from an object without the entirety of the objects table counting against the limit?

  • The ORDER BY LIMIT 1 should only count as one row; is the field an indexed field?
    – sfdcfox
    Dec 20, 2017 at 19:29
  • It's actually a formula field, I guess that since salesforce has to go through and evaluate the formula for each row it counts each one against me? Dec 20, 2017 at 19:31
  • Yes, formula fields result in a full table scan. You'll need to do your work asynchronously or figure a way to make it an indexed value (copy to a new field)?
    – sfdcfox
    Dec 20, 2017 at 19:34
  • Okay, Dokay, thanks for helping me think through that, if you want to put that as an answer, i'll mark it as correct Dec 20, 2017 at 19:35

1 Answer 1


Based on comments:

Filtering, ordering by (even with LIMIT), or grouping by a formula field always results in a full table scan. This will result in 50,000+ row errors or non-selective filter errors (e.g. when more than 100,000 rows exist). When possible, copy the values to an indexed field (say, via workflow rules), or run the task asynchronously, such as in a Batchable class.

You must log in to answer this question.

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