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
Rohit ShisodeRohit Shisode 

How to add pagination to a VF page that is using campaign standard list controller and a extension

If I use only standard list controller then pagination is working fine, but i need to sort list so I'm using extension and when i'm querying records in extension the pagination is not working, I'm new to VF, any help would be appriciated thanx.
code:
VF page
<apex:page standardController="Campaign" recordSetVar="Campaigns" extensions="CampaignCustomContoller">
    <apex:form >

        <apex:pageBlock title="User Driven Campaign" id="campaigns_list">
            
            Filter: 
            <apex:selectList value="{! filterId }" size="1">
                <apex:selectOptions value="{! listViewOptions }"/>
                <apex:actionSupport event="onchange" action="{!updatedCampaignsList}" reRender="campaigns_list"/>
            </apex:selectList>
            <apex:commandLink style="Float:right;" action="{!URLFOR($Action.Campaign.New)}" value="New" />
            <!-- Campaigns List -->  
            
            <apex:pageBlockTable value="{! Campaigns }" var="ct">
                <apex:column >
                    <apex:commandLink action="{! $Page.CampaignDetail}?id={! ct.id}" value="{!ct.Name}"/>
                </apex:column>
                
                <apex:column value="{! ct.Status}"/>
                <apex:column value="{! ct.StartDate}"/>
                <apex:column value="{! ct.LastModifiedDate}"/>
            </apex:pageBlockTable>
            <!-- Pagination -->
            <table style="width: 100%"><tr>
                <td align="center">
                    <!-- Previous page -->
                    <!-- active -->
                    <apex:commandLink action="{! Previous }" value="« Previous"
                         rendered="{! HasPrevious }"/>
                    <!-- inactive (no earlier pages) -->
                    <apex:outputText style="color: #ccc;" value="« Previous"
                         rendered="{! NOT(HasPrevious) }"/>
                    
                    &nbsp;&nbsp;  
                    
                    <!-- Next page -->
                    <!-- active -->
                    <apex:commandLink action="{! Next }" value="Next »"
                         rendered="{! HasNext }"/>
                    <!-- inactive (no more pages) -->
                    <apex:outputText style="color: #ccc;" value="Next »"
                         rendered="{! NOT(HasNext) }"/>
                </td>
            
            </tr></table>

        </apex:pageBlock>

    </apex:form>
</apex:page>

Extension::

public with sharing class CampaignCustomContoller {
    public List<System.SelectOption> listView{get;set;}
    ApexPages.StandardSetController standardCampaignController;
    Map<String,String> mapListView = new Map<String,String>();
    public string filterListId{get;set;}
    public List<Campaign> Campaigns {get;set;}
    public Boolean flag=true;
  
  /*Constructor*/
   public CampaignCustomContoller(ApexPages.StandardSetController controller){
       standardCampaignController = controller;
       Campaigns = controller.getRecords();
       listView = controller.getListViewOptions();
       filterListId = controller.getFilterId();
       flag=true;
       
       for(System.SelectOption s: listView){
           mapListView.put(s.getLabel(),s.getValue());
       }
       controller.setFilterId(mapListView.get('Recently Modified'));
       //updatedCampaignsList();
    }
    
   /*updates campaigns list on page*/
    public void updatedCampaignsList(){
        string selectedListView = 'LastModifiedDate';
        filterListId = standardCampaignController.getFilterId();
        //Campaigns = standardCampaignController.getRecords();
        string whereClauseString = '';
        if(flag||mapListView.get('Recently Modified') == filterListId){
            selectedListView = 'LastModifiedDate';
            flag=false;
        }else if(mapListView.get('All Campaigns') == filterListId){
            selectedListView = 'StartDate';
        }else if(mapListView.get('InActive Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = false ';
        }else if(mapListView.get('All Active Campaign') == filterListId){
            selectedListView = 'StartDate';
            whereClauseString = ' WHERE IsActive = true ';
        }
        string query = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign'+whereClauseString+' ORDER BY '+selectedListView+' DESC';
        //standardCampaignController = new ApexPages.StandardSetController(Database.getQueryLocator(query));
        Campaigns = Database.query(query);
    }
}


 
Best Answer chosen by Rohit Shisode
Javwad AzeemJavwad Azeem
Hi Rohit,

Here is sample code of Pagination using extension:-

Apex Controller:-
public with sharing class PaginationExtension {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 10;
                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
    
    Public List<Account> getAccounts(){
        List<Account> accList = new List<Account>();
        for(Account a : (List<Account>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
    
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
}

VF page:
<apex:page Controller="PaginationExtension">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.BillingCity}"/>
                <apex:column value="{!a.BillingState}"/>
                <apex:column value="{!a.BillingCountry}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please refer the below links:-
  • https://salesforce.stackexchange.com/questions/99745/controller-extension-wrapper-with-pagination-limit-50000-not-advancing
  • https://salesforce.stackexchange.com/questions/129793/pagination-does-not-displays-when-using-extension-controller-class
Regards,
Javwad Azeem

All Answers

Javwad AzeemJavwad Azeem
Hi Rohit,

Here is sample code of Pagination using extension:-

Apex Controller:-
public with sharing class PaginationExtension {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 10;
                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
    
    Public List<Account> getAccounts(){
        List<Account> accList = new List<Account>();
        for(Account a : (List<Account>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
    
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
}

VF page:
<apex:page Controller="PaginationExtension">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.BillingCity}"/>
                <apex:column value="{!a.BillingState}"/>
                <apex:column value="{!a.BillingCountry}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please refer the below links:-
  • https://salesforce.stackexchange.com/questions/99745/controller-extension-wrapper-with-pagination-limit-50000-not-advancing
  • https://salesforce.stackexchange.com/questions/129793/pagination-does-not-displays-when-using-extension-controller-class
Regards,
Javwad Azeem
This was selected as the best answer
Rohit ShisodeRohit Shisode
Thank you Javwad for answer, Here's code that worked for me:

VF page::

<apex:page controller="CampaignViewController">
    <apex:form >

        <apex:pageBlock title="User Driven Campaign" id="campaigns_list">
            
            Filter: 
            <apex:selectList value="{! campaignFilterId }" size="1">
                <apex:selectOptions value="{! CampaignExistingViews }"/>
                <apex:actionSupport event="onchange" action="{! resetFilter}" reRender="campaigns_list"/>
            </apex:selectList>
            <apex:commandLink style="Float:right;" action="{!URLFOR($Action.Campaign.New)}" value="New" />
            <!-- Campaigns List -->  
            
            <apex:pageBlockTable value="{! campaigns }" var="ct">
                <apex:column >
                    <apex:commandLink action="{! $Page.CampaignDetail}?id={! ct.id}" value="{!ct.Name}"/>
                </apex:column>
                
                <apex:column value="{! ct.Status}"/>
                <apex:column value="{! ct.StartDate}"/>
                <apex:column value="{! ct.LastModifiedDate}"/>
            </apex:pageBlockTable>
            <!-- Pagination -->
            <table style="width: 100%"><tr>
                <td align="center">
                    <apex:commandLink action="{! prev }" value="« Previous"/>
                    
                    &nbsp;&nbsp;  
                    <apex:commandLink action="{! Next }" value="Next »"/>
                </td>
            
            </tr></table>

        </apex:pageBlock>

    </apex:form>
</apex:page>


Controller::

public with sharing class CampaignViewController {
    private String baseQuery1 = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign ORDER BY LastModifiedDate DESC';
    private String baseQuery2 = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign ORDER BY StartDate DESC';
    private String baseQuery3 = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign WHERE isActive=true ORDER BY StartDate DESC';
    private String baseQuery4 = 'SELECT Name,Status,Type,StartDate,LastModifiedDate FROM Campaign WHERE isActive=false ORDER BY StartDate DESC';
    Map<String,String> mapListView = new Map<String,String>();
    public String campaignFilterId {get; set;}
    private Integer pageSize = 20;
    public CampaignViewController(){}
      public ApexPages.StandardSetController AccSetController {
            get{
                if(AccSetController == null){
                    AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery1));
                    AccSetController.setPageSize(pageSize);
                    if(campaignFilterId != null){
                       AccSetController.setFilterId(campaignFilterId);
                    }
                }
                 return AccSetController;
            }set;
      }
      
      public CampaignViewController(ApexPages.StandardSetController c) {}
        public void next(){
          if(AccSetController.getHasNext()){
              AccSetController.next();
          }
        }
        public void prev(){
           if(AccSetController.getHasPrevious()){
               AccSetController.previous();
           }
        }
        public List<Campaign> getCampaigns(){
            return (List<Campaign>)AccSetController.getRecords();
        }
        public SelectOption[] getCampaignExistingViews(){
           return AccSetController.getListViewOptions();
        }
        public pageReference resetFilter(){
            for(System.SelectOption s: AccSetController.getListViewOptions()){
                        mapListView.put(s.getLabel(),s.getValue());
            }
            if(mapListView.get('Recently Modified')==campaignFilterId){
                        System.debug('Inside recent If');
                        AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery1));
                    }else if(mapListView.get('All Campaigns')==campaignFilterId){
                        System.debug('Inside OTHERS else');
                        AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery2));
                    }else if(mapListView.get('All Active Campaigns')==campaignFilterId){
                        AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery3));
                    }else if(mapListView.get('InActive Campaign')==campaignFilterId){
                        AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(baseQuery4));
                    }
            AccSetController.setPageNumber(1);
            return null;
        }
}