Chat with us, powered by LiveChat

Blog

How To Create Pagination Within Salesforce

Salesforce DevelopmentAnother 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.

4

Step 1 –

Go To Setup → Develop → Pages

Click the “New” Button for creating new pages:

1

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

2

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:pageblockTable>
<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}”/>
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form></apex:page>

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;
else
return false;
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs)
return true;
else
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”.

3

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.

Tags: , ,

Calander Dec 2, 2013

One response to “How To Create Pagination Within Salesforce”

  1. sameer says:

    when you are going to last Page it’s give Error :-“2000 soql offset error”

    How to resolve it,Help me out.

Leave a Reply to sameer Cancel reply

Your email address will not be published. Required fields are marked *

Enter the text from the image below *