Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

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];


 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.

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?

1 Answer

0 votes
by (32.1k points)

I have modified the way the getClientAccountCount() method works. So basically, it is only going to be able to give an indication of how many rows there are as the aggregate functions are being 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);


Browse Categories