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
Salvatore ParacuolloSalvatore Paracuollo 

Help for few questions

Hello, I have a doubt on this question:

which code block returns the listview of an Account object using the following debug statement?
system.debug(controller.getListViewOptions());
 
A. ApexPages.StandardSetController controller = new Apexpages.StandardSetController([SELECT id FROM Account LIMIT 1]);
B. ApexPages.StandardController controller = new ApexPages.StandardController(Database.getQueryLocator('select id from account limit 1')).
C. ApexPages.StandardController controller = new ApexPages.StandardController([SELECT id FROM Account LIMIT 1]);
D. ApexPages.StandardSetController controller = new ApexPages.StandardSetController(database.getQueryLocator('select id from Account Limit 1'))


The correct answer seems to be D, but I didn0t understand why D and not A (trying by dev console, both worked): can anyone help me, please?
Alain CabonAlain Cabon
Hi,

 A and D should work as both constructors are correct.

[SELECT id FROM Account LIMIT 1] is ambiguous.

if ([SELECT Name FROM Account LIMIT 1] instanceof List<account>) {system.debug('test list ok');}  // OK

if ([SELECT Name FROM Account LIMIT 1] instanceof account) {system.debug('test account ok');}  // ERROR

List<account> accountList = [SELECT Name FROM Account LIMIT 1];
system.debug('accountList name : ' + accountList[0].name);

... but this also works (limit 1 is ambiguous): 

account acc = [SELECT Name FROM Account LIMIT 1];
system.debug('acc name : ' + acc.name);  // OK

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_constructors.htm

Regards
Alain CabonAlain Cabon
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_ctor_2.htm

List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);

or 

List<account> accountList = [SELECT Name FROM Account LIMIT 1];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);

or

ApexPages.StandardSetController ssc = new ApexPages.StandardSetController([SELECT Name FROM Account LIMIT 1]);

The all thing works as long as LIMIT 1 is an instance of List<Account> (check with instanceof)

Perhaps a very bad practice (ambiguous) but that works with LIMIT 1. 

Regards
Alain CabonAlain Cabon
When there is no account at all (zero account), perhaps D is still working but there is not this  basic assumption in the wording of the question.