How To Create Pagination Within Salesforce

By admin December 2, 2013
Salesforce Development, Services 3

Another question has been sent to us and we thought it would be good to share these step by step directions with the Salesforce development world. The question was how to do Pagination within Salesforce if I have 100,000 records. Pagination is the process of taking a complete document or a large number of records in this example and breaking that document/large number of records into separate pages for viewing within the Salesforce. For example, let’s say we want to display a list of records that only shows 10 records within each page rather than a huge set within Salesforce. The Salesforce user can click the next tab and go through each new page display the next 10 records. Please follow these step by step directions of how to make this pagination feature within your Salesforce CRM.


Step 1 –

Go To Setup → Develop → Pages

Click the “New” Button for creating new pages:


Step 2 – In The Label & Name Box Type “pagination”


Step 3 – In Visualforce editor paste the following code:

<apex:page controller=”Pagination” sidebar=”false” showHeader=”false”>
<apex:form >
<apex:pageBlock id=”details”>
<apex:pageblockTable value=”{!acclist}” var=”acc”>
<apex:column value=”{!acc.Name}”/>
<apex:column value=”{!acc.website}”/>
<apex:column value=”{!acc.AnnualRevenue}”/>
<apex:column value=”{!acc.Description}”/>
<apex:column value=”{!acc.Type}”/>
<apex:pageblockButtons >
<apex:commandButton value=”First Page” rerender=”details” action=”{!FirstPage}” disabled=”{!prev}”/>
<apex:commandButton value=”Previous” rerender=”details” action=”{!previous}” disabled=”{!prev}”/>
<apex:commandButton value=”Next” rerender=”details” action=”{!next}” disabled=”{!nxt}”/>
<apex:commandButton value=”Last Page” rerender=”details” action=”{!LastPage}” disabled=”{!nxt}”/>

Step 4 – Paste this controller code in Apex Class editor:

public class Pagination
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 10;
public Pagination()
totalRecs = [select count() from account];
public List<account> getacclist()
List<account> acc = Database.Query(‘SELECT Name, website, AnnualRevenue, description, Type FROM account LIMIT :LimitSize OFFSET :OffsetSize’);
System.debug(‘Values are ‘ + acc);
return acc;
public void FirstPage()
OffsetSize = 0;
public void previous()
OffsetSize = OffsetSize – LimitSize;
}public void next()
OffsetSize = OffsetSize + LimitSize;
}public void LastPage()
OffsetSize = totalrecs – math.mod(totalRecs,LimitSize);
public boolean getprev()
if(OffsetSize == 0)
return true;
return false;
public boolean getnxt()
if((OffsetSize + LimitSize) > totalRecs)
return true;
return false;


“Save” this code

Step 5 – Open this page in your Salesforce organization to check its working correctly: “https://ap1.salesforce.com/apex/Pagination”.


Step 6 – Enjoy! If you have any questions, please feel free to contact us as our expert Salesforce developers would be more than happy to assist you with any of your Salesforce development needs.

Related Posts