Woe Is the StandardSetController? Record set cannot be empty

I looked around for a duplicate for this issue, but couldn’t find one. I created a very simple test on my way towards testing my extension. For those interested, SObjectFactory is on GitHub. This is all it takes to fail:

static testMethod void testSSC()
{
    List<Case> records = SObjectFactory.create(Case.sObjectType, 10);
    system.assertEquals(10, records.size());
    for (SObject record : records) system.assertNotEquals(null, record.Id);

    ApexPages.StandardSetController controller =
        new ApexPages.StandardSetController(records);

    system.assertEquals(10, controller.getRecords().size());
    system.assert(controller.getSelected().isEmpty());
}

System.VisualforceException: Record set cannot be empty

Stack Trace: External entry point

Wait, what?!

The list is definitely not empty, and the records are already inserted. Why is the following so different? It passes, but I don’t understand the difference.

static testMethod void testSSC()
{
    List<Case> records = SObjectFactory.create(Case.sObjectType, 10);
    system.assertEquals(10, records.size());
    for (SObject record : records) system.assertNotEquals(null, record.Id);

    ApexPages.StandardSetController controller =
        new ApexPages.StandardSetController([SELECT Id FROM Case]);

    system.assertEquals(10, controller.getRecords().size());
    system.assert(controller.getSelected().isEmpty());
}

I did a little more digging and did find one post that sort of explains what is going on here… apparently the list construction must use a concrete type. Can anyone find official documentation supporting this claim?

static testMethod void works()
{
    List<SObject> records = new List<Case>();
    ApexPages.StandardSetController controller =
        new ApexPages.StandardSetController(records);
}
static testMethod void doesNotWork()
{
    List<Case> records = new List<SObject>();
    ApexPages.StandardSetController controller =
        new ApexPages.StandardSetController(records);
}

Answer

This doesn’t seem to be explicitly documented and the message is not the best…but the answer in the other post is correct, I verified this internally (I work at Salesforce).

Attribution
Source : Link , Question Author : Adrian Larson , Answer Author : PepeFloyd

Leave a Comment