Pagination and the StandardSetController – No Custom Controller Required

When I designed the code for my online course for lynda.com called “Developing with Visualforce“, I looked for solutions that were as simple as possible. After all, even I will admit that developers do tend to over complicate things unnecessarily.

One of the areas I was covering was handling multiple records and therefore pagination was introduced. In my research for the course, I uncovered dozens of posts/tutorials by developers using Custom Controllers or Extensions to implement this functionality. For a while I thought this was the only way it could be done. Since the underlying message for my course was to find the simplest solution possible, I am glad I dug a little farther and found a way to provide the necessary pagination without the need for a custom controller.

I discovered this while reading through the very excellent online resource titled, “Visualforce in Practice“. This very practical guide was published in 2013 and it contains many tips for development that were discovered through years of practice. Chapter 4 of this guide covers the Standard List Controller and the corresponding StandardSetController. What it revealed was that despite the fact that even the Salesforce documentation leads you to believe that you need a custom controller extension to change the page size, this is not actually. true. You can in fact do it using just the Standard Controller and referencing PageSize member variable. You can then change the page size by adding a command button with a NULL action to force the controller to update.

For example, the page that I created for the course, displayed multiple opportunities that could be edited by the user (see Figure below). At the bottom of the page, I included buttons to page the user through the results, but I also wanted to include a way for the user to change the default of 20 records per page.

EditOpportunities

This was accomplished by simply adding standard apex controls to the bottom of the page. No custom controller or extension required. All the code needed to produce the page you see above is as follows:

 <apex:page standardController="Opportunity" recordSetVar="opportunities" tabStyle="Opportunity">
    <apex:form >
        <apex:pageBlock title="Edit Opportunities" >
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!save}"/>
              <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!opportunities}" var="opp">
                <apex:column value="{!opp.name}"/>
                  <apex:column headerValue="Stage">
                    <apex:inputField value="{!opp.stageName}"/>
                  </apex:column>
                  <apex:column headerValue="Close Date">
                    <apex:inputField value="{!opp.closeDate}"/>
                  </apex:column>
            </apex:pageBlockTable>
            <apex:panelGrid cellpadding="5" cellspacing="5" columns="5" >
                <apex:commandButton value="|<" action="{!first}"  />
                <apex:commandButton value="<" action="{!previous}" rendered="{!HasPrevious}" />
                <apex:commandButton value=">" action="{!next}" rendered="{!HasNext}" />
                <apex:commandButton value=">|" action="{!last}"  />
                <apex:panelGroup >
                    <apex:outputText value="Records/Page"></apex:outputText>&nbsp;&nbsp;
                    <apex:selectList value="{!PageSize}" size="1">
                        <apex:selectOption itemValue="10" itemLabel="10"></apex:selectOption>
                        <apex:selectOption itemValue="15" itemLabel="15"></apex:selectOption>
                        <apex:selectOption itemValue="20" itemLabel="20"></apex:selectOption>
                    </apex:selectList>&nbsp;&nbsp;
                    <apex:commandButton action="{!NULL}" value="Update Page Size"/>
                </apex:panelGroup>
           </apex:panelGrid>
         </apex:pageBlock>
    </apex:form>
</apex:page>

I very much prefer this solution to the one that requires a custom controller. It is easier to maintain and does not involve creating unit tests to deploy. Hope this helps someone else to not over complicate code they are creating. If you are interested in learning more, check out my online course for lynda.com called, “Developing with Visualforce“.

10 thoughts on “Pagination and the StandardSetController – No Custom Controller Required

  1. Thanks Sara. This is one great example of how we can simplify things a lot if we really look into the standard offerings instead of doing custom code. Even I have written a lot of custom controller and extensions for pagination. There are very few examples out there talking about edits with standard set controllers. I was curious, does this method help in retaining changed values while paginating through the records?

  2. Sara –
    This is amazing and does exactly what I would want to do. My reason for using it is because I have created a filtered list that mimics the Standard Account Related List. The object that is being filtered is a custom object (Property__c). I have created an Apex Class Controller Extension to filter the Active Properties. I am then left with a list without pagination. I tried to apply your solution, and I get the controls at the bottom of the VF page, the new code saves with no errors, but the list of active properties does not respond to the VF page controls. Do have any recommendations for where I should look to make changes? On the Apex class? Thank you for your help.

  3. Thanks sara it’s really helpful to make a new idea for to me and my knowledge.And I have the question if like the above page if the sorting order is in only by the visualforce is to be possible with in the pagination if it’s possible please let to me know,Thank you Mohan

  4. Sara it’s taking default pageSize as 20, can we control it. Suppose i want to set default pageSize as 10.

      1. No, Sara i am talking about the pagesize when the page loads. It is loading with the pagesize 20, but i want the default page size would be 10 . i am not talking about dropdown list. Can we do that , if yes How?

  5. I see what you are asking now. To accomplish what you want you would need to use a custom controller and then assign the pageSize in the constructor of that controller.

    1. Yeah Sara, Thanks a lot . I implemented what i wanted with few changes in your page with adding Extension.

  6. Standard Controller variables

    Hi Sara,

    You used {!PageSize}. I cannot find that variable fined in the SalesForce Standard controller documentation https://developer.salesforce.com/docs/. What I did find was getPageSize method. So do we assume that the variablename is always after the get prefix? I understand get prefix is part of the getter method format. I was just trying to find a list of variables that standart controller has but it’s not out there it appears.

    Thanks,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s