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
App DevelopmentApp Development 

How to resolve issue with pagination?

I am facing issue with pagination as its not working correctly. its droping records. could you please help me out.
Here is my code.

Apex class: 


public with sharing class SearchController{

    public integer PageSize {get;set;}
    public integer PageNumber {get;set;}
    public integer RecordCount {get;set;}
    public List<contactwrapper> objAWList {get;set;}
    private Set<id> selectedContactIds;
    public boolean displayPopup {get; set;}  
    public List<contact> objSelectedContacts {get; set;}
    private String soql {get;set;}  
      
    public SearchController() {
        PageSize = 10;
        PageNumber = 1;   
        RecordCount = 0;
        selectedContactIds = new Set<id>();
        objAWList = new List<contactwrapper>();
        objSelectedContacts = new List<contact>();
          
    }
      

    public void closePopup() {        
        displayPopup = false;    
    }     
      

    public void showPopup() {  
        setSelectedContacts();      
        displayPopup = true;    
        objSelectedContacts  = GetSelectedcontacts();
    }
      
    public Integer getSelectedCount(){
        return this.selectedContactIds.size();
    }
      
    public List<contact> GetSelectedContacts(){    
        List<contact> objConList = [Select Id, Name,Email,Account.Name,Account_Cust__c,Job_Title__c 
                                    From Contact Where ID IN : selectedContactIds];       
        system.debug('objConList :'+objConList);
        return objConList;
    }
      
      
    private void setSelectedContacts()
    {
        for(contactwrapper item: objAWList)
        {
            if(item.IsSelected)
            {    
                if(!this.selectedContactIds.contains(item.ContactId))
                {
                    this.selectedContactIds.add(item.ContactId);
                }
            }
            else
            {
                if(this.selectedContactIds.contains(item.ContactId))
                {
                    this.selectedContactIds.remove(item.ContactId);
                }
            }
        }        
    }
      
    public integer PageIndex {
        get { return (PageNumber - 1); }
    }    
      
    public integer PageCount {
        get { return getPageCount(); }
    }
      
    public integer Offset {
        get { return (PageSize * PageIndex); }
    }
      
  
    public void runSearch() {
        objAWList = new List<contactwrapper>();
        try {
                List<Contact> objConList = new List<Contact>(); 
                String contactName = Apexpages.currentPage().getParameters().get('ContactName');
         
                if (!contactName.equals('')){
                    soql = 'select firstname, lastname, Name,account.name,Job_Title__c,Account_Cust__c from contact where account.name != null';
                    soql += ' and name LIKE \'%' + contactName + '%\'';
                    soql += ' order by firstName ASC limit '+PageSize +' offset '+Offset;
                    system.debug('soqlfinal::'+soql);
                    objConList = Database.query(soql);
                    RecordCount = objConList.size();
                    
                }else if(contactName.equals('')){
                    RecordCount = objConList.size();
                    objConList.clear();
                }
          
                 system.debug('objConList---'+objConList);                    
                 if(!objConList.isEmpty()){                         
                     for(Contact Con : objConList){
                            ContactWrapper objAW = new ContactWrapper();
                            objAW.ContactId = Con.Id;
                            objAW.ContactName = Con.Name;
                            objAW.AccountCust= Con.Account_Cust__c ;
                            objAW.JobTitle= Con.Job_Title__c;
                            
                            if(this.selectedContactIds.contains(con.Id)){
                                objAW.IsSelected = true;
                            }
                            else{
                                objAW.IsSelected = false;
                            }
                            objAWList.Add(objAW);
                     }
                 }             
             
        }
        catch (QueryException e) {
            ApexPages.addMessages(e);   
            
        }
    }
      
    public integer LNumber {
        get { return RecordCount == 0 ? 0 : (Offset + 1); }
    }
  
    public integer UNumber {
        get { 
            integer iUNum = ((LNumber + PageSize) - 1);
            return (iUnum > RecordCount) ? RecordCount : iUNum; 
        }
    }
      
    public boolean AllowMoveNext {
        get{ return ((PageIndex + 1) < PageCount); } } public boolean AllowMovePrev { get{ return (PageIndex > 0); }
    }
      
    public void Prev() {
        setSelectedContacts();
        PageNumber--;
  
        if (PageNumber <= 0) { PageNumber = 1; } } public void Next() { setSelectedContacts(); PageNumber++; if (PageNumber > PageCount) {
            PageNumber = PageCount;
        }
    }
      
    public void Last() {
        setSelectedContacts();
        PageNumber = PageCount; 
    }
  
    public void First() {
        setSelectedContacts();
        PageNumber = 1;
    }
      
    private integer getPageCount() {
        integer iPageCount = 1;
  
        if (RecordCount != 0 && PageSize != 0) {
            iPageCount = (RecordCount/PageSize) + ((Math.mod(RecordCount, PageSize)) > 0 ? 1 : 0);
        }
        return iPageCount;
    }
      
    public class Contactwrapper
    {
        public Id ContactId {get;set;}
        public String ContactName {get;set;}
        public String AccountCust {get;set;}
        public String JobTitle {get;set;}
        public Boolean IsSelected{get;set;}
        public Contactwrapper()
        {
    
        }
    }    
}
 
SonamSonam (Salesforce Developers) 
Hi,

Please try to update your code as shown in the working code below:

Class:
public with sharing class soql_offset_example {
    private integer counter=0;  //keeps track of the offset     private integer list_size=5; //sets the page size or number of rows     public integer total_size; //used to show user the total size of the list     public soql_offset_example() {         total_size = [select count() from Account]; //set the total size in the constructor     }     public Account[] getNumbers() {         try {             Account[] numbers = [select Id, name                                  from Account                                  order by name                                 limit :list_size                                  offset :counter];             return numbers;         } catch (QueryException e) {             ApexPages.addMessages(e);                return null;         }     }    public PageReference Beginning() { //user clicked beginning         counter = 0;         system.debug('------'+counter);         return null;    }    public PageReference Previous() { //user clicked previous button         counter -= list_size;         system.debug('------'+counter);         return null;    }    public PageReference Next() {        //user clicked next button         system.debug('------'+counter);         counter += list_size;         return null;    }    public PageReference End() { //user clicked end         counter = total_size - math.mod(total_size, list_size);         system.debug('------'+counter);         return null;    }     public Boolean getDisablePrevious() {          //this will disable the previous and beginning buttons         if (counter>0) return false; else return true;    }     public Boolean getDisableNext() { //this will disable the next and end buttons         if (counter + list_size < total_size)  return false; else return true;             }     public Integer getTotal_size() {         return total_size;    }     public Integer getPageNumber() {         return counter/list_size + 1;            }     public Integer getTotalPages() {         if (math.mod(total_size, list_size) > 0) {             return total_size/list_size + 1;         } else {             return (total_size/list_size);         }     } }


Visualforce Page:
<apex:page title="Salesforce SOQL Offset Example Using Visualforce" controller="soql_offset_example" showHeader="false" sidebar="false" readOnly="true" cache="false"> <apex:sectionHeader subtitle="SOQL Offset Example" title="Square Root Table"/> <apex:pageBlock > <apex:pageBlockButtons location="top" > <apex:outputPanel id="myButtons"> <apex:form > <apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/> <apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/> <apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="myPanel,myButtons"/> <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="myPanel,myButtons"/> </apex:form> </apex:outputPanel> </apex:pageBlockButtons> <apex:pageBlockSection title="Numbers and their Square Roots (Total List Size: {!total_size})" collapsible="false"> <apex:outputPanel id="myPanel"> <apex:pageMessages id="theMessages" /> <apex:pageBlockTable value="{!numbers}" var="n" align="center"> <apex:column value="{!n.id}" /> <apex:column value="{!n.name}" /> <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet> </apex:pageBlockTable> </apex:outputPanel> </apex:pageBlockSection> </apex:pageBlock> </apex:page>


Please try this and let me know if this VF and class helps you update your code to get the pagination working.