5

I'm getting this error everytime I'm assigning a non empty list of sObject to a standardsetcontroller

list<sObject> listSearchArticles = someOtherMethod(); // someOtherMethod returns a list of sObjects
if(listSearchArticles.size() > 0 ){
    this.setCon = new ApexPages.StandardSetController( listSearchArticles );
    this.setCon.setpageNumber(1);
    this.setCon.setPageSize(25);
} 

2 Answers 2

7

This is a a small curveball, he must just be constructed with concrete list type.

Instead of:

new ApexPages.StandardSetController(new List<SObject>());
//System.VisualforceException: Record set cannot be empty

Try this:

new ApexPages.StandardSetController(new List<Document>());
//ok!

Same goes if you have an empty query result set. Cast the collection before constructing.

1
  • How did you determine that the list type must be concrete? Is that documented somewhere?
    – Adrian Larson
    Commented Mar 11, 2016 at 5:20
0

You could add a null check before assigning the list to your standardset controller. What you are doing currently is size check. Try this

list<sObject> listSearchArticles = someOtherMethod(); // someOtherMethod returns a list of sObjects
if(listSearchArticles != NULL && listSearchArticles.size() > 0 ){
    this.setCon = new ApexPages.StandardSetController( listSearchArticles );
    this.setCon.setpageNumber(1);
    this.setCon.setPageSize(25);
}
else {
    this.setCon = new ApexPages.StandardSetController(new List<sObject>());
}

If the listSearchArticles is null or its size is zero, the standardsetcontroller will be initialized with blank list of sobjects. Otherwise it will be initialized with the list of sobjects.

Hope it helps.

2
  • I've tried adding a null check as well :( Doesn't work Commented Jun 21, 2015 at 12:30
  • This answer simply isn't correct - it's not throwing an error because the list is empty, it's throwing an error because the list of of sObjects rather than a concrete implementation. bigassforce's answer is correct. Commented Aug 31, 2017 at 10:19

You must log in to answer this question.

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