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
mayurguptamayurgupta 

Want to have Pagination in my Visualforce page. Unable to achive it

I have tried using pagination code in my Apex class & VF page. But not able to get result of pagination. Kindly help me out. I am attching the Apex class and Visual force page which i am using.

//Apex Class Begin


public class SearchForAccountsController {
    public String accountResults { get; set; }
    //Public static integer check = 0;
    Account[] accounts;
   
    String searchCriteriaValue{ get; set; }//=SFDCConstants.BLANK_VALUE;
    String testValue = SFDCConstants.BLANK_VALUE;
    String searchValue = SFDCConstants.BLANK_VALUE;    
    Integer recordSize = 0;
    String sessionId = SFDCConstants.CONSTANT_ZERO;
    String message = SFDCConstants.BLANK_VALUE;
    Integer accountLength;    
    public Boolean onLoadAcc{ get; set; }
    public Boolean exact{ get; set; }
    String sc;
    public String getsc() {
        return this.sc;
     }
    public void setsc(String sc){
        this.sc = sc;
    }
    
    // Start Pagination code Mayur
    
    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }
 
    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
 
    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
 
    // returns the first page of records
    public void first() {
        con.first();
    }
 
    // returns the last page of records
    public void last() {
        con.last();
    }
 
    // returns the previous page of records
    public void previous() {
        con.previous();
    }
 
    // returns the next page of records
    public void next() {
        con.next();
    }
    
    // End Pagination code Mayur
   
        
   /**
   * Default constructor to set the values of searchCriteriaValue and sessionId from the current page parameters. 
   */
   public SearchForAccountsController() { 
                
        sc = ApexPages.currentPage().getParameters().get('sc');
        exact = TRUE;
        onLoadAcc = FALSE;
        system.debug('the URL is***'+ApexPages.currentPage().getURL());
        if(sc == NULL || sc == '')
            searchCriteriaValue = ApexPages.currentPage().getParameters().get(SFDCConstants.SEARCH_CRITERIA_PARAM_NAME); 
        else
            searchCriteriaValue = sc;
                    
        sessionId = ApexPages.currentPage().getParameters().get(SFDCConstants.SESSION_ID_PARAM_NAME);
        
        if(sessionId == null){
            sessionId = SFDCConstants.CONSTANT_ZERO;
        }
        string crt =  ApexPages.currentPage().getParameters().get('crt');
        if(crt != NULL && crt != '')
            if(crt.contains('TRUE'))
                onLoadAcc =  TRUE;   
            else
                onLoadAcc = FALSE;
                
        string exact1 =  ApexPages.currentPage().getParameters().get('exact');
        if(exact1 != NULL && exact1 != '')
            if(exact1.contains('TRUE'))
                exact =  TRUE;   
            else
                exact = FALSE;
       
         
        system.debug('Search criteria constructor:::::'+searchCriteriaValue);        
        sc = searchCriteriaValue;
        
        }
     
       
    /**
    * Method to redirect the user to account search result page.
    *
    * @return Returns the pageRef.
    */
    public PageReference save() {                
        PageReference pageRef = new PageReference(SFDCConstants.ACCOUNT_SEARCH_PATH+searchCriteriaValue);
        list<Account> accChk = new list<Account>();
        if(searchCriteriaValue.length() >=2){
            accChk = [SELECT Id FROM Account WHERE Name =: searchCriteriaValue]; 
            system.debug('The Result is :::'+accChk);
            if(!accChk.isEmpty())
                pageRef.getParameters().put('exact','TRUE');
            else
                pageRef.getParameters().put('exact','FALSE');
            pageRef.getParameters().put('crt','TRUE');
        }
        sc = searchCriteriaValue;            

        system.debug('Search criteria save:::::'+sc);
        pageRef.setRedirect(true);
        return pageRef;
    }
    
       // instantiate the StandardSetController from a query locator
        public ApexPages.StandardSetController con {
        get {
            if(con == null) {
            searchValue  = SFDCConstants.CONSTANT_PERCENT+searchCriteriaValue+SFDCConstants.CONSTANT_PERCENT;
            searchValue  = searchValue.replace(SFDCConstants.CONSTANT_ASTRICK,SFDCConstants.CONSTANT_PERCENT);
            searchValue  = searchValue.replace(' ', '%');
                con      = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name, Id FROM Account a where a.Name 
                            like :searchValue or a.OCN__c like :searchValue order by a.Name]));
                // sets the number of records in each page set
                con.setPageSize(3);
            }
            return con;
        }
        set;
    }
     
    
    /*public void checkCriteriaChange(){
        onLoadAcc = FALSE;
        }   */
    /**
    * Getter to fetch all the accounts corresponding to search criteria.
    *
    * @return Returns the accounts.
    */
    public Account[] getAccounts(){   
        accounts = null; 
         
        // checks whether the session id and search criteria are not equal to null or blank.
        if(sessionId != SFDCConstants.CONSTANT_ZERO && searchCriteriaValue == null || searchCriteriaValue == SFDCConstants.BLANK_VALUE || 
                    (searchCriteriaValue != null && searchCriteriaValue.length() < 2)){
            message = SFDCConstants.SEARCH_STRING_ERROR_MESSAGE;
        } 
        // checks whether the search criteria is not equal to null or blank.
        else if(searchCriteriaValue != null && searchCriteriaValue != SFDCConstants.BLANK_VALUE && sessionId != SFDCConstants.CONSTANT_ZERO){             
            testValue  = SFDCConstants.CONSTANT_PERCENT+searchCriteriaValue+SFDCConstants.CONSTANT_PERCENT;
            testValue  = testValue.replace(SFDCConstants.CONSTANT_ASTRICK,SFDCConstants.CONSTANT_PERCENT);
            testValue = testValue.replace(' ', '%'); 
            accounts = [SELECT a.Sub_Segment__c, a.Owner.FirstName, a.Owner.LastName, a.OwnerId, a.OCN__c, 
                            a.Name, a.Division__c, a.Country__c, a.Channel__c, a.Type,a.Status__c FROM Account a where a.Name 
                            like :testValue or a.OCN__c like :testValue order by a.Name limit 51];               
            recordSize = accounts.size();
           // accounts.setpageSize(2);
            
            // check whether the search result size is zero or not
            if(recordSize == 0){
                message = SFDCConstants.SEARCH_RECORD_SIZE_ZERO_ERROR_MESSAGE;
            }
            // check whether the search result size is greater than 50 or not
            else if(recordSize > 50){
                message =SFDCConstants.SEARCH_RECORD_SIZE_50_ERROR_MESSAGE;
                accounts[recordSize - 1] = null;
            }else{
                message =SFDCConstants.BLANK_VALUE;                    
            }
        }
        // checks whether the session Id is not equal to zero .
        else if(sessionId != SFDCConstants.CONSTANT_ZERO){
            message =SFDCConstants.SEARCH_SESSION_ID_ERROR_MESSAGE; 
        } 
        else{
            message = SFDCConstants.BLANK_VALUE;
            sessionId = SFDCConstants.CONSTANT_ONE;
        }        
        if(recordSize == 0){
            accounts = null;
        }
               //if(check == 1)
               // onLoadAcc = TRUE;
                //check++;
                
         System.debug('Accounts are ' + accounts);  
                return accounts;
        
    }
    /**
    * Getter for error message.
    *
    * @return Returns the message.
    */
    public String getMessage(){        
        return message;
    }   
    /**
    * Getter to calculate the size of account search result.
    *
    * @return Returns the account size.
    */
    public Integer getAccountLength(){
        if(accounts == null){
            return 0;
        }
        return accounts.size();
    }
    /**
    * Getter for search criteria.
    *
    * @return Returns the searchCriteriaValue.
    */
    public String getSearchCriteriaValue(){
        return searchCriteriaValue;
    }
    /**
     * Mutator for the searchCriteriaValue
     *
     * @param searchCriteriaValue The searchCriteriaValue to set.
     */
    public void setSearchCriteriaValue(String searchCriteriaValue){
        this.searchCriteriaValue = searchCriteriaValue;
    }
    /**
     * Mutator for the sessionId
     *
     * @param searchCriteriaValue1 The sessionId to set.
     */
    public void setSessionId(String sessionId){
        this.sessionId = sessionId;
    } 
    
    /**
    * Method to redirect the user to Create account page.
    *
    * @return Returns the pageRef.
    */
    public PageReference CreateAccount() {                
        Account acc = new Account();
        acc.Name = searchCriteriaValue;
        insert acc;
        pageReference ref = new pageReference('/'+acc.id+'/e?retURL=%2F'+acc.id+'&saveURL=/apex/Address&save_new_url=/apex/Address?requestFlag=SANE');
        ref.setRedirect(TRUE);
        return ref;
    }  
}

// Apex Class End
// Visual-force page Begin

<apex:page controller="SearchForAccountsController" extensions="HooversSearchForAccountsController" tabstyle="Account">

<div style="background: #b1b1b1;padding: 5px 10px;">
<p><b>Search for Accounts</b></p>
</div>

<p style="color:red;"><apex:outputPanel rendered="{!NOT(forUpdate)}"> {!message}</apex:outputPanel></p>
<apex:messages style="color:red; font-size:13px;"/>
<apex:form id="frm">
<div style="background: #d9d9d9;padding: 15px 5px;">
           <script>
               function enterEvent(e){
                   if(e.keyCode == 13){
                       return false;
                   }
               }
           </script>     
           <p> <b>Search </b>&nbsp;&nbsp; <apex:inputText value="{!searchCriteriaValue}" style="width:300px" rendered="{!NOT(forUpdate)}" onkeypress="return enterEvent(event)">
                                              <!--apex:actionSupport event="onchange" immediate="true" reRender="frm:HoovSrch" action="{!checkCriteriaChange}"/-->  
                                          </apex:inputText> 
                                          <apex:inputText value="{!searchKey}" style="width:300px" rendered="{!forUpdate}" onkeypress="return enterEvent(event)"/>
            <apex:commandButton action="{!save}" value="search" rerender="msgs" accesskey="s" rendered="{!NOT(forUpdate)}"/>
            <apex:commandButton action="{!SearchHoovers}" value="Search in Hoovers" rendered="{!forUpdate}"/></p>
          
        
        
</div><br></br>

 <apex:outputPanel rendered="{!NOT(forUpdate)}">   
 <apex:pageBlock title="Accounts"  rendered="{!AND(NOT(ISNULL(accounts)),accounts.size>0)}">
  
    <div align="right" style="display:{!IF(NOT(ISNULL(accounts)),'block','none')}">
    <font size="1pt">Page #:&nbsp;<apex:outputLabel value="{!pageNumber}"/></font>
   </div> 
    
    
    <br/><br/> 


  <apex:pageBlockTable value="{!accounts}" var="q" style="font-size: 10pt; "> 

                <apex:column headerValue="OCN">{!q.OCN__c}</apex:column>
                <apex:column headerValue="Account Name"><a href="/{!q.id}">{!q.Name}</a></apex:column>
                <apex:column headerValue="Account Owner">{!q.Owner.FirstName} {!q.Owner.LastName}</apex:column>
                <apex:column headerValue="Division">{!q.Division__c}</apex:column>
                <apex:column headerValue="SubSegment">{!q.Sub_Segment__c}</apex:column>
                <apex:column headerValue="Channel">{!q.Channel__c}</apex:column>
                <apex:column headerValue="Country">{!q.Country__c}</apex:column>
                <apex:column headerValue="Type">{!q.Type}</apex:column>           
                <apex:column headerValue="Status">{!q.Status__c}</apex:column>           
           </apex:pageBlockTable> 
           
    <apex:panelGrid columns="4">
        <apex:commandLink action="{!first}">First</apex:commandlink>
        <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink>
        <apex:commandLink action="{!next}" rendered="{!hasNext}" >Next</apex:commandlink>
        <apex:commandLink action="{!last}">Last</apex:commandlink>
    </apex:panelGrid>
 </apex:pageBlock> 


 </apex:outputPanel>
 
 <apex:pageBlock id="pgB" title="Hoovers Search Results" rendered="{!AND(NOT(ISNULL(accountResults)),accountResults.size>0)}">
  
  <apex:pageBlockTable value="{!accountResults}" var="h" style="font-size: 10pt; "> 

                <apex:column headerValue="Company Name">
                    <apex:outputLink value="{!$Page.Hoovers_AccountSearchDetails}">{!h.CompanyName}<apex:param name="DUNS" value="{!h.Duns}" /><apex:param name="sc" value="{!IF(OR(sc==NULL,AND(searchKey!=NULL,sc!=searchkey)), searchKey, sc)}" /><apex:param name="accid" value="{!accId}" />
                    </apex:outputLink>
                </apex:column>
                <apex:column headerValue="Location">{!h.Location}</apex:column>
                <apex:column headerValue="DUNS">{!h.Duns}</apex:column>
                           
           </apex:pageBlockTable> 
 
 </apex:pageBlock>
 
 <apex:outputPanel rendered="{!NOT(forUpdate)}">
   <apex:commandButton value="New Account" disabled="{!NOT(AND(OR(ISNULL(accounts),accounts.size=0), OR(ISNULL(accountResults),accountResults.size=0), onLoadHvr))}" action="{!CreateAccount}"/>  
   <!--apex:commandButton value="New Account" action="{!CreateAccount}"/-->
   <apex:commandButton id="HoovSrch" value="Search in Hoovers" disabled="{!NOT(AND(OR(ISNULL(accounts),accounts.size=0), OR(ISNULL(accountResults),accountResults.size=0), onLoadAcc) || NOT(exact))}" action="{!SearchHoovers}"/>
 </apex:outputPanel>
 
 </apex:form> 
  
</apex:page>

// Visualforce page End

 

 

mayurguptamayurgupta

Any update? Can someone help me out.