Reusable pagination in Visualforce page using standardsetcontroller

    It is a common requirement to paginate over a set of data in Visualforce page. There are different methods to implement a custom pagination in Visualforce pages. Most common methods are standardsetcontroller pagination and offset pagination. In this article we will go over standardsetcontroller pagination and its advantages.

Standardsetcontroller pagination

    In standardsetcontroller pagination, you reuse the methods of standardsetcontroller provided by Salesforce and extend it with your own features. This pagination helps you to navigate up to 10k records. You can make this approach very reusable by creating a generic parent class with option to set query and use standardsetcontroller methods. Then the controller of the page where you need pagination should inherit this utility class. You can find a sample for reusable utility class below,

Main query will be passed to the constructor of this class from child controller. Thereafter if required getWhereClause() and getSortClause() methods can be overridden from the child controller. A simple example of child controller reusing this pagination for pagination over account records can be found below,

You can see that the code in the controller is very less and we are just reusing most of the pagination from parent utility class. Actual page implementation can be found below,

Conclusion

    It is a very reusable method of pagination. Here you are reusing many methods from standardsetcontroller. So there is very less chance for something to go wrong. Also in order to reuse the pagination for a different object, for example contacts, the process is very simple. Only things that you need to change in the controller are the query that is being passed to parent utility class, return type and casted type of getter method. Also standardsetcontroller pagination gives ability to paginate up to 10k records, whereas custom offset pagination can be used only up to 2k records.

No comments:

Post a Comment