5

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

1 Answer 1

1

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

2
  • 1
    So it's just undocumented but true? I'm surprised casting doesn't even work. It would be nice to get a little more depth. Any chance of that?
    – Adrian Larson
    Mar 11, 2016 at 21:13
  • Is there anyone you (or any of us) can bug to bring more clarity to this situation? This is a pretty disappointing shortcoming in our understanding of a 10+ year old component.
    – Adrian Larson
    Mar 17, 2016 at 19:52

You must log in to answer this question.

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