I have a Visualforce page where I'd like to display a count of the number of records in a particular sObject table.
In the Visualforce page I'd have something fairly simple, like:
<p>Client Account Count: {!ClientAccountCount}</p>
Then in the controller:
// Return the number of clients
public integer getClientAccountCount() {
return [Select count() from Account where SomeCustomField__c = 'Client' limit 50000];
}
I thought with the limit clause in the SOQL I'd be fine as it would only every return a maximum of 50,000. However, in practice I still get this exception in the production org:
09:29:12:179 SOQL_EXECUTE_BEGIN [108]|Aggregations:0|select count() from Account where SomeCustomField__c = 'Client' limit 50000
09:29:12:331 EXCEPTION_THROWN [108]|System.LimitException: Too many query rows: 50001
Is there a safe way to perform this query that won't result in an exception that I can't catch?
Oddly, if I try the following as anonymous apex in production it works just fine and returns 50,000.
integer count = [select count() from Account where SomeCustomField__c = 'Client' limit 50000];
Perhaps the issue is the cumulative number of query rows across all operations that is causing the problem and I need to check the Limits in code before running the query?
There is a similar post on the Force.com discussion boards - Too many query rows on COUNT(*) function. I can't set the VF page to read only to increase the query row limit.