function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SeanMSSeanMS 

Problem with Paging in my Custom Controller

I have a custom controller for a list display. I've seen a lot on the discussion boards about how when using a custom controller that you have to make use of the StandardSetController methods in order to get Pagination working. I've tried to set this up, but I'm having trouble. When the list is first loaded, all is good (the first ten records are displayed), but when I click the 'next' button the entire recordset is loaded in addition to the first ten records (instead of the next ten records, we now have duplicates and 30 records). Can someone please point me in the right direction for correcting this?

 

TIA!

 

Controller Code:

 

public with sharing class OrderListController2
{
List<OrderListRowWrapper> orderList = new List<OrderListRowWrapper>();

public ApexPages.StandardSetController setCon
{
get {
if (setCon == null)
{
setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Order_ID__c
FROM Order_Details__c
ORDER BY LastModifiedDate DESC]));

setCon.setPageSize(10);
}
return setCon;
}
set;
}

public OrderListController2() {
}

public OrderListController2(ApexPages.StandardSetController controller) {
}

public List<OrderListRowWrapper> getOrderList()
{
Order_Details__c[] orderListRecords = (Order_Details__c[])setCon.getRecords();

if (orderListRecords != null)
{
// For each Order List Row...
for (Order_Details__c thisOrderListItem : orderListRecords)
{
OrderListRowWrapper thisRowWrapper = new OrderListRowWrapper(thisOrderListItem.Order_ID__c);
orderList.Add(thisRowWrapper);
}
}

return orderList;
}

public PageReference nextPage() {
setCon.next();
return null;
}

public PageReference prevPage() {
setCon.previous();
return null;
}

public List<sObject> dataObjects {
get {
return setCon.getRecords();
}
}

public Integer currentPage {
get {
return setCon.getPageNumber();
}
}

public Integer totalPages {
get {
return setCon.getResultSize();
}
}

public Boolean hasPrevious {
get {
return setCon.getHasPrevious();
}
}

public Boolean hasNext {
get {
return setCon.getHasNext();
}
}

public PageReference moveNext() {
setCon.next();
return null;
}

public PageReference movePrevious() {
setCon.previous();
return null;
}

}

 

VisualForce Page:

 

 

<apex:page controller="OrderListController2" title="Orders">
<style type="text/css">
// TO DO
</style>
<apex:form id="navigation">
<table bgcolor="white" width="100%">
<tr>
<td width="160"><img src="{!$Resource.HeaderLogo}" /></td>
<td align="left">
<apex:actionRegion >
<apex:outputText value="Records per page: "></apex:outputText>
<apex:selectList value="{!setCon.pagesize}" size="1">
<apex:selectOption itemLabel="10" itemValue="10"></apex:selectOption>
<apex:selectOption itemLabel="20" itemValue="20"></apex:selectOption>
<apex:selectOption itemLabel="50" itemValue="50"></apex:selectOption>
<apex:selectOption itemLabel="100" itemValue="100"></apex:selectOption>
<apex:actionSupport event="onchange" rerender="thePage, navigation" status="theStatus" />
</apex:selectList>
<BR />
<apex:actionStatus id="theStatus" startText="Updating the list..." stopText="Total records: {!setCon.resultSize}" />
</apex:actionRegion>
</td>
<td align="right">
<apex:outputPanel styleClass="prevNext" layout="block" rendered="{!OR(setCon.hasNext,setCon.hasPrevious)}">
<apex:outputPanel rendered="{!NOT(setCon.hasPrevious)}" styleClass="greyedLink">&lt; Previous Page</apex:outputPanel>
<apex:commandLink rendered="{!setCon.hasPrevious}" action="{!movePrevious}" rerender="thePage">&lt; Previous Page</apex:commandLink>
<span> | </span>
<apex:outputPanel rendered="{!NOT(setCon.hasNext)}" styleClass="greyedLink">Next Page &gt;</apex:outputPanel>
<apex:commandLink rendered="{!setCon.hasNext}" action="{!moveNext}" rerender="thePage">Next Page &gt;</apex:commandLink>
</apex:outputPanel>

<!-- <apex:panelGrid columns="2" id="navTop">
<apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
<apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink>
</apex:panelGrid>
-->
</td>
</tr>
<tr><td colspan="3"></td></tr>
</table>
</apex:form>
<apex:pageBlock id="thePage" title="Orders">
<apex:form id="theForm">
<!--<apex:pageBlockSection >-->
<!--<apex:pageBlockTable var="o" value="{!orders}">-->
<apex:pageBlockTable var="o" value="{!orderList}" id="theOrderList">
<apex:column >
<apex:facet name="header"><b>Order #</b></apex:facet>
<a href="/{!o.Id}" target="_self">{!o.OrderID}</a>
</apex:column>
<apex:column >
<apex:facet name="header"><b>Date</b></apex:facet>
<a href="/{!o.Id}" target="_self">{!o.OrderDate}</a>
</apex:column>
<apex:column >
<apex:facet name="header"><b>Customer</b></apex:facet>
{!o.CustomerName}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Total ($)</b></apex:facet>
${!o.Total}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Status</b></apex:facet>
{!o.Status}
</apex:column>
<apex:column >
<apex:facet name="header"><b>P/E Date</b></apex:facet>
{!o.PEDate}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Shipped</b></apex:facet>
{!o.ShippedDate}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Site</b></apex:facet>
{!o.Site}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Order Source</b></apex:facet>
{!o.OrderSource}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Tran ID</b></apex:facet>
{!o.TransactionId}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Promo Code</b></apex:facet>
{!o.PromoCode}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Order Type</b></apex:facet>
{!o.BillingType}
</apex:column>
<apex:column >
<apex:facet name="header"><b>Salesperson</b></apex:facet>
{!o.Salesperson}
</apex:column>
</apex:pageBlockTable>
<!--</apex:pageBlockSection>-->
<apex:outputPanel styleClass="prevNext" layout="block" rendered="{!OR(setCon.hasNext,setCon.hasPrevious)}">
<apex:outputPanel rendered="{!NOT(setCon.hasPrevious)}" styleClass="greyedLink">&lt; Previous Page</apex:outputPanel>
<apex:commandLink rendered="{!setCon.hasPrevious}" action="{!prevPage}" rerender="thePage">&lt; Previous Page</apex:commandLink>
<span> | </span>
<apex:outputPanel rendered="{!NOT(setCon.hasNext)}" styleClass="greyedLink">Next Page &gt;</apex:outputPanel>
<apex:commandLink rendered="{!setCon.hasNext}" action="{!nextPage}" rerender="thePage">Next Page &gt;</apex:commandLink>
</apex:outputPanel>
</apex:form>
</apex:pageBlock>
</apex:page>

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

A couple of things spring to mind:

 

(1) Should you not be clearing out the orderList in the nextPage/prevPage methods - otherwise you are adding the next page worth of data on top of your existing data.

(2) Setters and getters can be called multiple times when a page is requested, which will lead to duplicates.  I'd suggest you change your getOrderList method to only retrieve the data from the setController if the orderList is empty.