7

I would like to run this query:

SELECT MAX(Auto_Number__c) MaxNumber FROM Opportunity

But I am getting this error:

System.LimitException: Too many query rows: 50001

Because there are more than a 100,000 records.

Would adding a ORDER BY and LIMIT to the query still ensure I get the MAX?

SELECT MAX(Auto_Number__c) MaxNumber FROM Opportunity ORDER BY Auto_Number__c DESC LIMIT 1

But this give me:

Ordered field must be grouped or aggregated: Auto_Number__c (129:16)

16

Take out the MAX from your query:

SELECT Auto_Number__c FROM Opportunity ORDER BY Auto_Number__c DESC LIMIT 1
  • 1
    I feel stupid.. – Robs May 8 '19 at 14:17
  • 3
    @Robs We've all had moments like that, no need to feel bad about it 😁 – sfdcfox May 8 '19 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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