1

EDIT:

I have implemented the changes here based on the Daniel Ballinger now I'm not getting error but when I click on the Next button is not going to the next page i have around 25 rows.

here is my retrofit code:

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

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

Retrofit Products Method:

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;
    } 

END

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;
    }
}

this is how I load the data in wrapper class: and the error i'm getting:

> Constructor not defined: [ProductWrapper].<Constructor>(SObject,
> Asset_Line_Item__c)

Constructor:

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

Loading data in wrapper:

list<ProductWrapper> pws = new list<ProductWrapper>();
for(sObject p : 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);
     pws.add(pw);
}

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
  • 1
    change for(sObject p : this.con.getRecords()) { to for(asset_line_items__c p : (List<asset_line_items__c>)this.con.getRecords()) { Sep 18, 2016 at 3:39
  • 1
    While wrapper definition you used asset_line_items__c but while passing values you used Asset_Line_Item__c, because of this compiler is not able to find the right definition and it is saying constructor is not available. Sep 18, 2016 at 3:49

1 Answer 1

2

You need to cast the sObject records returned by the StandartSetController to Product2 records.

E.g.

ProductWrapper pw = new ProductWrapper((Product2)p,b);

Alternatively, you could do the conversion on the for loop.

for(Product2 p : (List<Product2>)this.con.getRecords()) {  
    //...
}
3
  • I'm not getting the error now but when I click on the next button its not going to the next page, I have updated my question with detail, please have a look - thanks
    – Nick
    Sep 18, 2016 at 14:55
  • @NickKahn Ideally you would close this question out as solved if the initial problem is resolved and then start a new question if you have encountered a new problem. Sep 18, 2016 at 20:53
  • 1
    Fair enough, I have created the new question and I will accept this as answer. here is the question I have posted: salesforce.stackexchange.com/questions/141031/…
    – Nick
    Sep 18, 2016 at 21:29

You must log in to answer this question.

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