[SalesForce] Content cannot be displayed: Too many query locator rows: 10001

How to show a message/convey to the user that there are more than 10000 records when using StandardSetController?

Do I need to fire a separate SOQL just to count the number of rows and use the LIMT clause in the StandardSetController query to limit the rows? Is there a better way?

Also what is the use of the method getCompleteResult() ? I thought I could use this method to see if there are more than 10000 records in the StandardSet but when there are more than 10000 the page simply errors out with the message "Content cannot be displayed: Too many query locator rows: 10001".

Best Answer

If I take an org with over 50000 Account records and run this snippet, I don't get any errors and I do get the expected output:

ApexPages.StandardSetController controller = new ApexPages.StandardSetController([
    SELECT Id FROM Account LIMIT 50000
]);

system.debug(controller.getCompleteResult()); // false
system.debug(controller.getResultSize()); // 10000

And I also get the expected output if the LIMIT is at or below 10000.

ApexPages.StandardSetController controller = new ApexPages.StandardSetController([
    SELECT Id FROM Account LIMIT 10000
]);

system.debug(controller.getCompleteResult()); // true
system.debug(controller.getResultSize()); // 10000

I tested these with API Version set all the way down to 10 on my Apex Class and 15 on my Visualforce Page, and did not receive an error. Perhaps you open another query locator somewhere else on your page somewhere.

<apex:page controller="Throwaway">
    <apex:outputText value="{!IF(controller.completeResult, 'complete', 'partial')}" />
</apex:page>

<!--
public with sharing class Throwaway
{
    public ApexPages.StandardSetController controller
    {
        get
        {
            if (controller == null)
                controller = new ApexPages.StandardSetController([
                    SELECT Id FROM Account LIMIT 50000
                ]);
            return controller;
        }
        private set;
    }
}
-->
Related Topic