New Way of Pagination

   In salesforce there are 2 ways to achieve pagination.

1) Using StandardSetController (Limitation : To instantiate         
    StandardSetController we can query up to 10000 records using query
    locator.   Querying more than 10000 records causes a LimitException to
    be thrown.
    However by passing list of more than 10000 records we can instantiate
    StandardSetController, again holding more records in the List and instantiate StandardSetController can reach heap size. )

2) Using OFFSET in SOQL query (Limitation : We can skip max 2000
    records using OFFSET which means we cannot do pagination by using
    OFFSET if the query skips more than 2000 records. Skipping more than
    2000 records causes "Maximum SOQL offset allowed is 2000 error" . )
   
    Here is a new way to achieve it. Hold records you need to display
    by   pagination  in List of List of SObjects (i.e List<List<SObject>>).
    Where the size of List<SObject> is equal to pageSize.
    Rest of the code is self explanatory.

Visualforce Page (Name : PaginationByList)
=======================================================================
    <apex:page controller="PaginationByListController">
        <apex:pageBlock >
        <apex:pageBlockTable Id="table" value="{!accList}" var="a">
        <apex:column headerValue="Account Number" value="{!a.AccountNumber}"/>
        <apex:column headerValue="Name" value="{!a.Name}"/>
        <apex:column headerValue="Phone" value="{!a.Phone}"/>
        </apex:pageBlockTable>
         <apex:form >
          <apex:selectList value="{!paginationSize}" size="1">
         <apex:actionSupport event="onchange" action="{!setAccountList}" rerender="table"/>
         <apex:selectOption itemValue="3"></apex:selectOption>
         <apex:selectOption itemValue="6"></apex:selectOption>
         <apex:selectOption itemValue="9"></apex:selectOption>
         </apex:selectList>
       
        <apex:CommandButton action="{!next}" value="Next" rendered="{!hasNext}"/> 
        <apex:CommandButton action="{!previous}" value="Previous" rendered="{!hasPrevious}"/>
          </apex:form>
     
        </apex:pageBlock>
    </apex:page>


======================================================================

Apex Controller(Name : PaginationByListController)
======================================================================


    public with sharing class PaginationByListController {
        public List<Account> accList {get; set;}
        public Integer paginationSize {get; set;}
        public Integer listNumber;
        List<List<Account>> ListOfListAccount;
   
        public PaginationByListController()
        {
            paginationSize = 3;
            setAccountList();
       
        }
        public void setAccountList()
        {
            integer i = 0;
            listNumber = 0;
            System.debug('paginationSize'+paginationSize);
            ListOfListAccount = new List<List<Account>>();
            for(Account a : [Select Id, Name, AccountNumber, Phone from Account])
            {
                if(math.mod(i, paginationSize) == 0)
                {
                    List<Account> tempAccountList = new List<Account>();
                    tempAccountList.add(a);
                    ListOfListAccount.add(tempAccountList);
                }
                else
                {
                    ListOfListAccount.get(ListOfListAccount.Size() - 1).add(a);
               
                }
                i++;
           
            }
            accList = ListOfListAccount.get(listNumber);
           
        }
        public void next()
        {
            if(ListOfListAccount.Size()-1 > listNumber)
            accList = ListOfListAccount.get(++listNumber);       
        }
   
        public void previous()
        {
            if(0 < listNumber)
            accList = ListOfListAccount.get(--listNumber);       
        }
   
        public boolean gethasPrevious()
        {
            if(listNumber > 0)
            return true;   
            return false;
        }
   
        public boolean gethasNext()
        {
            if(listNumber < ListOfListAccount.Size() -1)
            return true;   
            return false;
        }
       
    }

======================================================================

     So you might have questioned that, are we able to overcome limitations of  pagination we are facing in existing approaches. We can say that we have partially overcomes limitations of existing approaches because, by using this methodology we can query up to 50000 records and do Pagination unless we are not reaching the heap size limit which is 6 MB.
     If you see any flaw in this way or you have any suggestion  please comment.



Comments

  1. Hi,

    This is showing error when clicking the next button or changing the total number of rows. I have put the debug logs as well. The problem that I found was in the variable listnumber.
    Please let me know how we can resolve this.

    ReplyDelete
  2. I have more than 50000 records then how to handle pagination
    what i did was i am setting a auto number field in the object based on that i am trying to query them ..
    auto number field is text so i inserted another number formula field which converts this into number and stores it

    unable to proceed after that when showing search results the no's are not in sequence so paging becomes headache

    ReplyDelete

Post a Comment

Popular posts from this blog

Uploading multiple files as attachments using chaining of actionfunction in single click.

Dynamic Visualforce Bindings with Example

Two Columns Multi-Select Picklist Lightning Component