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

  • How did you determine that the list type must be concrete? Is that documented somewhere? – Adrian Larson Mar 11 '16 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.

  • I've tried adding a null check as well :( Doesn't work – cloud vandana Jun 21 '15 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. – Rob Baillie Aug 31 '17 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.