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
Tyler HarrisTyler Harris 

ApexPages.StandardSetController Passing in List Variable?

I'm getting the error "Constructor not defined: [ApexPages.StandardSetController].<Constructor>(List<StoreFront2.DisplayMerchandise). I want to pass in the list of Products I've constructed in the List<DisplayMerchandise> products; variable. How can I accomplish this? Or is there a better way to code this?  Thanks.
 
public class StoreFront2{

List<DisplayMerchandise> products;


public StoreFront2(){
        ct=new Purchase_Line_Items__c();
        ct2=new Contact();
        flag = false;
        flip = false;
        flag2 = true; 
      
         
    }

public ApexPages.StandardSetController samplePagination{
        get{
            if(samplePagination == null){
                samplePagination = new ApexPages.StandardSetController(products);
                samplePagination.setPageSize(5);
            }
            return samplePagination;
        }
        set;
        
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

   public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }
}

 
Best Answer chosen by Tyler Harris
Amit Chaudhary 8Amit Chaudhary 8
Hi Tyler,

As per per "standardsetcontroller" You can instantiate a StandardSetController in either of the following ways :-
From a list of sObjects
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
From a query locator
ApexPages.StandardSetController ssc = 
new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));
http://amitsalesforce.blogspot.in/2015/04/pagination-using-standardsetcontroller.html

Problem in your code.

You are trying to pass list of wrapper classes not sobject. If you will pass below list it will work
List<Merchandise__c> ListItem =   [SELECT id, name, description__c, price__c FROM Merchandise__c WHERE Total_Inventory__c > 0] ;
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(ListItem);
Or please check below post how to perform pagination on Wrapper class
http://amitsalesforce.blogspot.in/2014/11/pagination-with-wrapper-class-with.html


Let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Naval Sharma4Naval Sharma4
Hi Tyler,

Replace your method code with the below code.
public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = (List<DisplayMerchandise>)samplePagination.getRecords();
            }
        }
        return products;
    }

Please let me know if you get the same issue.

Thanks,
Naval
Tyler HarrisTyler Harris
Hi Naval,

Still getting the same error on the same line 19.
Amit Chaudhary 8Amit Chaudhary 8
Hi Tyler,

As per per "standardsetcontroller" You can instantiate a StandardSetController in either of the following ways :-
From a list of sObjects
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
From a query locator
ApexPages.StandardSetController ssc = 
new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));
http://amitsalesforce.blogspot.in/2015/04/pagination-using-standardsetcontroller.html

Problem in your code.

You are trying to pass list of wrapper classes not sobject. If you will pass below list it will work
List<Merchandise__c> ListItem =   [SELECT id, name, description__c, price__c FROM Merchandise__c WHERE Total_Inventory__c > 0] ;
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(ListItem);
Or please check below post how to perform pagination on Wrapper class
http://amitsalesforce.blogspot.in/2014/11/pagination-with-wrapper-class-with.html


Let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Tyler HarrisTyler Harris
Thank you Amit! This is exactly what I was looking for.
Nithesh KNithesh K
Hi Tyler, 

Replace your method code with the below code.
 
public ApexPages.StandardSetController samplePagination{
        get{
            if(samplePagination == null){
                List<Merchandise__c> Merchandiselist=[SELECT id, name, description__c, price__c 
                                                                            FROM Merchandise__c 
                                                                            WHERE Total_Inventory__c > 0];
                samplePagination = new ApexPages.StandardSetController(Merchandiselist);
                samplePagination.setPageSize(5);
            }
            return samplePagination;
        }
        set;        
    }
 
public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
            for (Merchandise__c item :(List<Merchandise__c>)samplePagination.getRecords()   ) {           
                products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

this is will surely work for you. 
if you want to do some navigation on record set , Example  
 
<apex:commandButton status="" reRender="" value="Next" action="{!next}" disabled="{!!hasNext}" title="Next Page" />

public void next() {
        setCon.next();
            // do you operation here 
 }

so that you can get next record as well you can perform your pration too.