LimitException: Too many query lines: 50001 from count () aggregate function
I have a Visualforce page where I would like to display the number of records in a specific sObject table.
On the Visualforce page, I'll have something pretty 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 that with the limit clause in SOQL I would be fine as only each would return a maximum of 50,000. However, in practice I still get this exception in the org:
09: 29: 12: 179 SOQL_EXECUTE_BEGIN [108] | Aggregation: 0 | select count () from account where SomeCustomField__c = 'Client' limit is 50000
09: 29: 12: 331 EXCEPTION_THROWN [108] | System.LimitException: Too Many Query Strings: 50001
Is there a safe way to fulfill this request that won't throw an exception that I can't catch?
Oddly enough, if I try the following as an anonymous vertex in production, it works fine and returns 50,000.
integer count = [select count() from Account where SomeCustomField__c = 'Client' limit 50000];
Perhaps the problem is the cumulative number of query lines across all operations that are causing the problem and I need to check the limits in the code before running the query?
There is a similar article on Force.com message boards - Too Many Query Strings in COUNT (*) Function . I am unable to tweak the read-only VF page to increase the query string limit.
source to share
Doh! I'm pretty sure I need to check the cumulative count of records received by SOQL queries for a query. So while a single SOQL query can get up to 50,000 records, two cannot execute 50,000.
Guess I can use Limits.getQueryRows () and Limits.getLimitQueryRows () to disable SOQL queries if needed.
I changed the getClientAccountCount () method. I believe that only everyone can give an idea of โโhow many rows there are, since aggregate functions are limited.
// Return the number of ad book clients
public string getClientAccountCount() {
System.debug(LoggingLevel.Debug, 'getClientAccountCount() - Current Query Rows: ' + Limits.getQueryRows() + '/' + Limits.getLimitQueryRows());
integer recordCount = [Select count() from Account where SomeCustomField__c = 'Client' limit 1001];
if(recordCount == 1001) { return '1000+'; }
return string.valueOf(recordCount);
}
This idea - Count the SOQL count () query as a single line query seems to make sense.
source to share