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“.

Developing with Visualforce Course just went Live!!!

The Visualforce course I designed for Lynda.com, Developing with Visualforce just went live. You can access it here and even if you are not a member, you can view 4 videos for free.  DevlopingWithVisuaforce

I am pretty proud of the course. I spent a lot of time trying to think through the best way to teach this stuff to beginners. I hope that I succeeded in some small way. I would love to know what you think, good or bad. I don’t mind getting bad feedback because it helps me to get better.

This is just the beginning too. I plan on doing a whole series of courses. This one was an introductory course, so keep that in mind if you are already very familiar with Visualforce. I stressed using declarative solutions and use of standard controllers whenever possible. I really do believe in the KISS (Keep it Simple Stupid) methodology.

Check out one of the free videos Lynda.com makes available:

http://www.lynda.com/player/embed/197238?fs=3&tr=utm_medium=ldc-partner;utm_source=SSPRC;utm_content=11889;utm_campaign=CD16309;aid=CD16309;bid=11889&w=560&h=315;subID5=197238&ps=paused

Oh, and this is the info on the course:

Course Description:
Start developing custom interfaces for the Force.com platform (including Salesforce) using Visualforce. Sara Morgan walks you through the basics of Visualforce, explores its MVC backbone, issues instructions for downloading a free developer edition of the software, and helps you decide if Visualforce is right for your organization. She then explores how to build pages out of common components; enhance pages with tabs, controllers, and extensions; and use the Developer Console to perform more advanced operations—such as editing multiple records, configuring error handling, and unit testing your code. Along the way, you’ll encounter a series of coding challenges that let you practice and expand your Visualforce coding skills.

Topics Include:
What is Visualforce?
Registering for an account
Building a Visualforce page
Building a tabbed page
Configuring permissions
Using standard list controllers and custom controllers
Exploring unit testing

Duration:
3h 4m