I want to achieve pagination on a visualforce page with the help of custom controller,right now my code is not working as expected. Can anyone please help me in what I am doing wrong?

My code is as follows:

<apex:page controller="AccountSearchClass">
<apex:form >
    <apex:inputText value="{!searchVariable}" />
    <apex:commandButton action="{!search}" value="Search" reRender="rr"/>
    <apex:pageBlock id="rr">
        <apex:pageBlockTable value="{!accObj}" var="b"  >
            <apex:column value="{!b.Name}" />
            <apex:column value="{!b.Phone}" />
            <apex:column value="{!b.AccountNumber}" />        
    <apex:commandButton action="{!search}" value="Search" reRender="rr"/>
    <apex:commandButton value="First Page" rerender="rr" action="{!FirstPage}" disabled="{!prev}"/>
    <apex:commandButton value="Previous" rerender="rr" action="{!previous}" disabled="{!prev}"/>
    <apex:commandButton value="Next" rerender="rr" action="{!next}" disabled="{!nxt}"/>

Custom controller:

public with sharing class AccountSearchClass {
public String searchVariable { get; set; }
public Integer OffsetSize=0;
public Integer LimitSize=5;
public Integer Totalrec=0;
private String temp;
public List <Account> accObj {get; set;}
public void search() {
    temp = 'Select id ,Name,AccountNumber,Phone from Account WHERE Name Like \'%'+searchVariable+'%\' OR Phone Like \'%'+searchVariable+'%\' ';
    accObj = database.query(temp);
public void FirstPage()
    OffsetSize = 0;
public void previous()
    OffsetSize =  LimitSize-OffsetSize;
public void next()
    OffsetSize = OffsetSize + LimitSize;
public boolean getprev()
    if(OffsetSize == 0)
        return true;
        return false;
public boolean getnxt()
    if((OffsetSize + LimitSize) > Totalrec)
        return true;
        return false;
  • What exactly is not working? – sfdcFanBoy Jun 14 '16 at 4:40
  • the next button is not showing up when i preview the page – Shobhit Saxena Jun 14 '16 at 4:46
  • Also all the records are getting displayed at the first page only. – Shobhit Saxena Jun 14 '16 at 4:53
  • yes that's because you didn't use offset and limit in your query. I added an answer. – sfdcFanBoy Jun 14 '16 at 4:57

There are quite a lot of missing things in your code

  1. Add search(); at the end of each of these 3 methods : FirstPage(), Next(), Previous()
  2. Your temp query is missing OFFSET AND LIMITat the end of the query. Add it correctly using the variables OffsetSize and LimitSize (LIMIT LimitSize OFFSET OffsetSize)
  3. All your apex:command buttons should be within the pageblock! So, end your <apex:pageblock> after the commandbuttons.

Follow the Example in this link.


Update your code accordingly. It should work.

  • that's ok, No problem! – sfdcFanBoy Jun 14 '16 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.