1

My question is continuation of this question

The problem I'm facing now is that when I click on the Next button its not showing the next page of set and I have 25 rows, I have setPageSize = 5

In the visualforce page i have action in the form like this:

<apex:page action="{!init}" ....

Apex code:

public List<ProductWrapper> Products 
     {
      get 
      {
                List<Product2> products2 = new List<Product2>(); 
                list<ProductWrapper> pws = new list<ProductWrapper>();                 
               for(Product2 p : (List<Product2>)this.con.getRecords()) {  

                  if(p.Asset_Line_items__r.isEmpty()){
                     pws.add(New ProductWrapper(p,New Asset_Line_Item__c(Quantity__c = 0));
                     continue;
                 }
                   for(asset_line_items__c b : p.asset_line_items__r) { 
                        //ProductWrapper pw = new ProductWrapper(p,b);
                        ProductWrapper pw = new ProductWrapper((Product2)p,b);
                        pws.add(pw);
                 }
           }    
           Products2 = pws;
           return Products2;
        }
        set;
    } 

Here is wrapper class:

public class ProductWrapper {

    public Product2 product {get;set;}
    public asset_line_items__c ali {get;set;}    

    public ProductWrapper(Product2 p, asset_line_items__c ali) {
        product = p;
        this.ali = ali;
    }
}

Constructor:

 public void init() {
        this.con = new ApexPages.StandardSetController(SOQL);
        this.con.setpageNumber(1);
        this.con.setPageSize(5);
    }

Pagination code:

    public void first() {
         con.first();
     } 
     public void last() {
         con.last();
     } 
     public void previous() {
         con.previous();
     }

     public void next() {
         con.next();
     }


 public ApexPages.StandardSetController con;

    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    public Integer totalPageNumber {
        get {
            Decimal totalSize = this.con.getResultSize();
            Decimal pageSize = this.con.getPageSize();
            Decimal pages = totalSize/pageSize;
            return (Integer)pages.round(System.RoundingMode.CEILING);
        }
        set;
    }

Visualforce page:

<apex:pageBlock id="pb">
  <apex:pageBlockTable value="{!products}" var="a">                     
          <apex:column headerValue="quantity" >                            
                <apex:inputField value="{!a.ali.qty}" required="true" />                    
          </apex:column>
          <apex:column value="{!a.Name}" headerValue="Name"/> 
          <apex:column value="{!a.ProductName}" headerValue="P Name"/> 
  </apex:pageBlockTable>
</apex:pageBlock> 

<apex:outputPanel rendered="{!products.size>0}" styleClass="center">
                <apex:commandButton value="|< First" action="{!first}" rerender="properties" rendered="{!HasPrevious}" />
                <apex:commandButton value="Previous" action="{!previous}" rendered="{!HasPrevious}"  rerender="properties"/>
                Page {!pageNumber} of {!totalPageNumber}
                <apex:commandButton value="Next" action="{!next}" rendered="{!HasNext}"  rerender="properties"/>
                <apex:commandButton value="Last>|" action="{!last}" rendered="{!HasNext}" rerender="properties" />
            </apex:outputPanel>
2
  • Can you clarify what the expected versus actual behavior is? Your post does not make either particularly clear.
    – Adrian Larson
    Sep 18, 2016 at 22:01
  • Hi Adrian, sorry about that, I'm trying to implement pagination using a wrapper class, it loads correct size of pages when the VF page loads but when I click on the Next button to go to the next page then it does not do anything, please let me know if you need any other clarification.
    – Nick
    Sep 18, 2016 at 22:04

2 Answers 2

1

I suspect the problem is with this line:

<apex:page action="{!init}" ....

And the corresponding controller method:

public void init() {
    this.con = new ApexPages.StandardSetController(SOQL);
    this.con.setpageNumber(1);
    this.con.setPageSize(5);
}

From the apex:page documentation for the action attribute:

This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

I suspect this is getting called when you are attempting to page the results, which is resetting the paging. Check you debug log to see what is going on when attempting to page the results.

There is a good example of implementing pagination using the StandardSetController in Visualforce page with pagination.

8
  • I removed the action from the page and place it in the constructor but still the same behavior .
    – Nick
    Sep 18, 2016 at 22:09
  • What does the debug log show when you call the next() method in the controller? Sep 18, 2016 at 22:11
  • does not show any debug, and i see its invoking the action method on next but its not showing any debug, that's what its puzzling me.. System.Debug('next: ' + next());
    – Nick
    Sep 18, 2016 at 22:12
  • Try modifying your next() method to start with System.assert(false, 'next() called'); It should break the execution as soon as that method is called via the commandButton action. Sep 18, 2016 at 22:33
  • nothing break public void next() { System.assert(false, 'next() called'); con.next(); }
    – Nick
    Sep 18, 2016 at 22:37
1

Yes, after ruining my Sunday finally found the cause of this insane silently failing pagination, I wish Salesforce could have more appropriate error message or some sort of indication to show in my case and yes I do have <apex:pageMessages ></apex:pageMessages> but still did not show any errors whatsoever.

The cause of my problem was:

When you fire an action via a button or command link it submits the entire form, meaning all the required fields in the form are checked.

Use use tags to split up the form into various groups of fields with their associated actions—when you fire an action in a region only the fields in that same region are sent.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .