Salesforce Pagination Technical Explanation
Pagination is used to display a list of records from an object in salesforce using standardset controller,
Once you done setting up standardController attribute then you need to set attribute value to recordSetVar attribute value on the same component.recordSetVar indicates the page uses a list controller.
Also we need to use ‘ApexPages.StandardSetController’ in controller extension constructor for inheriting prebuilt salesforce standard object functionality.
Now we can see an example on Pagination,
Apex Class:-
In the below Class, i am getting list of records in constructor using salesforce api method getRecord() and doing an explicit type casting to Account object ‘(Account)controller.getRecord()’
Then in ‘accountRecords’ getter method i am forming a query inside Database.getQueryLocator method which will return me list of accounts whose opportunity is not closed.
Again using getAccountPagination() getter method i am retrieving a list of records from accountRecords instance using salesforce api method ‘getrecords()’
public with sharing class AccountPagination {
private final Account acct;
public AccountPagination(ApexPages.StandardSetController controller) {
this.acct = (Account)controller.getRecord();
}
public ApexPages.StandardSetController accountRecords {
get {
if(accountRecords == null) {
accountRecords = new ApexPages.StandardSetController(
Database.getQueryLocator([SELECT Name FROM Account WHERE Id NOT IN
(SELECT AccountId FROM Opportunity WHERE IsClosed = true)]));
}
return accountRecords;
}
private set;
}
public List<Account> getAccountPagination() {
return (List<Account>) accountRecords.getRecords();
}
}
private final Account acct;
public AccountPagination(ApexPages.StandardSetController controller) {
this.acct = (Account)controller.getRecord();
}
public ApexPages.StandardSetController accountRecords {
get {
if(accountRecords == null) {
accountRecords = new ApexPages.StandardSetController(
Database.getQueryLocator([SELECT Name FROM Account WHERE Id NOT IN
(SELECT AccountId FROM Opportunity WHERE IsClosed = true)]));
}
return accountRecords;
}
private set;
}
public List<Account> getAccountPagination() {
return (List<Account>) accountRecords.getRecords();
}
}
VF Page:-
In this VF page, i am just iterating over the accountPagination getter method variable using VF <apex:dataList> tag which returns me a list of account records whose associated opportunity is not closed.
<apex:page standardController=”Account” recordSetVar=”accounts” extensions=”AccountPagination”>
<apex:pageBlock title=”Viewing Accounts”>
<apex:form id=”theForm”>
<apex:pageBlockSection >
<apex:dataList value=”{!accountPagination}” var=”acct” type=”1″>
{!acct.name}
</apex:dataList>
</apex:pageBlockSection>
<apex:panelGrid columns=”2″>
<apex:commandLink action=”{!previous}”>Previous</apex:commandlink>
<apex:commandLink action=”{!next}”>Next</apex:commandlink>
</apex:panelGrid>
</apex:form>
</apex:pageBlock>
</apex:page>
<apex:pageBlock title=”Viewing Accounts”>
<apex:form id=”theForm”>
<apex:pageBlockSection >
<apex:dataList value=”{!accountPagination}” var=”acct” type=”1″>
{!acct.name}
</apex:dataList>
</apex:pageBlockSection>
<apex:panelGrid columns=”2″>
<apex:commandLink action=”{!previous}”>Previous</apex:commandlink>
<apex:commandLink action=”{!next}”>Next</apex:commandlink>
</apex:panelGrid>
</apex:form>
</apex:pageBlock>
</apex:page>
Comments