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
calvin_nrcalvin_nr 

Some issues with pagination standard controller.

Hi folks, I implemented pagination on my visual force page looking at these two resources.

http://hisrinu.wordpress.com/2012/01/09/pagination-using-standardsetcontroller/

http://blog.jeffdouglas.com/2009/07/14/visualforce-page-with-pagination/

 

The next(), previous() etc methods do not work unless I make a call to the method which fetches records from the controller.

 

For example:

    // returns the first page of records
    public void first() { 
        //con = null;    
        try{
        con.first();  
              
        moneyTransactions = getMoneyTransactions();
        }
        catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
        
    }

 

 

Also the next, previous etc links start freezing up after I click on them continuously for some time. Has anybody encountered these problems before since these methods are pretty standard wrt to the standard set controller.

 

What am I doing wrong?

 

Thanks, Calvin

Suresh RaghuramSuresh Raghuram

my guess is since you are pressing continously the resposnse from the backend getting delayed and when u keep pressing the next and previous buttons request for the records may  quee up and getting hang when the quee size crossed.

calvin_nrcalvin_nr

^^So is there a solution to prevent this from occuring. 

 

I am using a List of objects to store the results. Should I be doing anything else?

calvin_nrcalvin_nr

The complete controller code.

public with sharing class FundingReportController { 
 
  // the soql without the order and limit
  private String soql {get;set;}
 
  Public Integer size{get;set;}
  Public Integer noOfRecords{get; set;} 
 
  
  //export to excel - returns a page reference to the AccountDataExcel page
    public PageReference exportToExcel() {
      return Page.fundingreportExcel;
    }
  

  // the collection of money transactions to display
  //public List<MoneyTransactionWrapper> moneyTransactions{get;set;}
   public List<Money_Transaction__c> moneyTransactions{get;set;}
  
    // instantiate the StandardSetController from a query locator
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
            size=10;
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Inside StandardSetController '+soql + ' order by ' + sortField + ' ' + sortDir));
                con = new ApexPages.StandardSetController(Database.getQueryLocator(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 100'));
                // sets the number of records in each page set
                con.setPageSize(size);
                noOfRecords = con.getResultSize();
            }
            return con;
        }
        set;
    }
 
 // returns a list of wrapper objects for the sObjects in the current page set
    public List<Money_Transaction__c> getMoneyTransactions() {
    try{
        //moneyTransactions = new List<MoneyTransactionWrapper>();
        moneyTransactions = new List<Money_Transaction__c>();
        for (Money_Transaction__c mt: (List<Money_Transaction__c>)con.getRecords()) 
         {              
            moneyTransactions.add(mt); 
         }       
           
        }
          catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
        return moneyTransactions ;
    }
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Settlement_Date_First__c'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    //get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    get { return soql + ' ' + sortDir; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public FundingReportController() {
    //Default dates: 6 months before today
    Date fromDate = date.today();
    fromDate = fromDate.addMonths(-6);
    String fromDateStr = String.ValueOf(fromDate);
    //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,fromDateStr   ));
    //Default dates: Today
    Date toDate = date.today();    
    String toDateStr = String.ValueOf(toDate);
    
   
  
    //soql = 'select ACH_Type__c, Settlement_Date_First__c, Total_ACH_Amount__c from Money_Transaction__c';
     //soql = 'select ACH_Type__c, Settlement_Date_First__c,MAX(Total_ACH_Amount__c) from Money_Transaction__c where Settlement_Date_First__c <= 2012-04-01 AND Settlement_Date_First__c >= 2012-01-01 group by ACH_Type__c,Settlement_Date_First__c ORDER BY MAX(Total_ACH_Amount__c)';
     //soql = 'select ACH_Type__c, Settlement_Date_First__c,Total_ACH_Amount__c,Settlement__r.id,Settlement__r.name,Money_Movement_Type__c,Bank_Name__c,Bank_Account_Number__c,Tax_Batch__c from Money_Transaction__c where Settlement_Date_First__c <= 2012-05-01 AND Settlement_Date_First__c >= 2012-01-01' + ' AND ACH_Type__c != \'VHR DDP Disbursement\'';
     soql = 'select ACH_Type__c, Settlement_Date_First__c, Total_ACH_Amount__c,Settlement__r.id,Settlement__r.name,Money_Movement_Type__c,Bank_Name__c,Bank_Account_Number__c,Tax_Batch__c,Payroll_Group_Detail__c from Money_Transaction__c where Settlement_Date_First__c <= '+ toDateStr + ' AND Settlement_Date_First__c >= ' + fromDateStr + ' AND ACH_Type__c != \'VHR DDP Disbursement\'';
     //soql = 'select ACH_Type__c, Settlement_Date_First__c,Total_ACH_Amount__c,Settlement__r.id,Settlement__r.name,Money_Movement_Type__c,Bank_Name__c,Bank_Account_Number__c,Tax_Batch__c from Money_Transaction__c where Settlement_Date_First__c <= 2012-05-01 AND Settlement_Date_First__c >= 2012-01-01' + ' AND ACH_Type__c != \'VHR DDP Disbursement\'' + ' AND Account__c =\'' + userAccountID +'\'';     
     //soql+= ' order by ' + sortField + ' ' + sortDir + ' limit 20';
     
     runQuery();
  }
 
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
  //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Inside Toggle'+soql + sortField +sortDir ));
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
     //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Query before setController '+soql ));
    
     con = null;    
     //soql = soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20';   
     moneyTransactions = getMoneyTransactions();
      //moneyTransactions = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
       //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'After money transactions is obtained '+soql ));
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops! SOQL error '+soql ));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String fromDate = Apexpages.currentPage().getParameters().get('fromDate');
    String toDate = Apexpages.currentPage().getParameters().get('toDate');
     //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,fromDate+toDate  ));
    //Date fromDate = Date.valueOf(fromDateStr);
    //Date toDate = Date.valueOf(toDateStr);
    //String accountName = Apexpages.currentPage().getParameters().get('accountName');
    //String technology = Apexpages.currentPage().getParameters().get('technology');
 
    //soql = 'select ACH_Type__c, Settlement_Date_First__c, Total_ACH_Amount__c from Money_Transaction__c';
    
    
    //soql = 'select ACH_Type__c, Settlement_Date_First__c, Total_ACH_Amount__c,Settlement__c from Money_Transaction__c where Settlement_Date_First__c <= '+ toDate + ' AND Settlement_Date_First__c >= ' + fromDate + ' AND ACH_Type__c != \'VHR DDP Disbursement\'' + ' order by ' + sortField + ' ' + sortDir + ' limit 20';
    soql = 'select ACH_Type__c, Settlement_Date_First__c, Total_ACH_Amount__c,Settlement__r.id,Settlement__r.name,Money_Movement_Type__c,Bank_Name__c,Bank_Account_Number__c,Tax_Batch__c,Payroll_Group_Detail__c from Money_Transaction__c where Settlement_Date_First__c <= '+ toDate + ' AND Settlement_Date_First__c >= ' + fromDate + ' AND ACH_Type__c != \'VHR DDP Disbursement\'';
    con = new ApexPages.StandardSetController(Database.getQueryLocator(soql)); 
    //soql = 'select ACH_Type__c, Settlement_Date_First__c,Total_ACH_Amount__c,Settlement__c from Money_Transaction__c where Settlement_Date_First__c <= 2012-05-01 AND Settlement_Date_First__c >= 2012-01-01';
    /*if (!firstName.equals(''))
      soql += ' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
    if (!lastName.equals(''))
      soql += ' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
    if (!accountName.equals(''))
      soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';  
    if (!technology.equals(''))
      soql += ' and interested_technologies__c includes (\''+technology+'\')';*/
 
    // run the query again
    runQuery();
 
    return null;
  }
  
     
    // 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 = null;    
        try{
        con.first();  
              
        moneyTransactions = getMoneyTransactions();
        }
        catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
        
    }
 
    // returns the last page of records
    public void last() {   
        //con = null;
        try{
        con.last();        
        moneyTransactions = getMoneyTransactions();
        }
        catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
    }
 
    // returns the previous page of records
    public void previous() {   
        //con = null;
         try{
            con.previous();      
            moneyTransactions = getMoneyTransactions();
            }
            catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
        }
 
    // returns the next page of records
    public void next() {  
    try{   
        //con = null;
        //if(con.getRecord()!=null)
        //{
           // con.save();
        //}
        con.next();      
        moneyTransactions = getMoneyTransactions();
        }
         catch (Exception e) 
            {
               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Code Error '+e));
            }
         
    }

 

calvin_nrcalvin_nr

Hi friends I will really appreciate any pointers here. 

 

Thanks.