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)

1 Answer 1

17

Take out the MAX from your query:

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

You must log in to answer this question.

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