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
Daniel Fernando SoaresDaniel Fernando Soares 

What's the differente between normal inline SOQL query and Database.getQueryLocator() for a custom StandardSetController?

Hi everyone!
I'm studuing for my Platform Developer I exam and came across with a question that got me confused:

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

According to the source from where I got this question, the right answer is D. Ok, B and C is obviously wrong because of the StandardController instead of StandardSetController, but then I ran some tests in the anonymous block using these 3 syntaxes:

1)
ApexPages.StandardSetController controller = new ApexPages.StandardSetController([SELECT Id FROM Account]);
2) 
ApexPages.StandardSetController controller = new ApexPages.StandardSetController(Database.getQueryLocator('SELECT Id FROM Account'));

3)
ApexPages.StandardSetController controller3 = new ApexPages.StandardSetController(Database.query('SELECT Id FROM Account'));

And I found out that all of them work the same way when calling the "system.debug(controller.getListViewOptions());", the debug was exactly the same in all these 3 cases.
So what's the difference between these 3 syntaxes for instantiating a StandardSetController?
Are answers A and D both right or is there really a reason for D to be more correct than A?

I appreciate any help!!
Best Answer chosen by Daniel Fernando Soares
sandeep madhavsandeep madhav
HI,

Hope this helps,

Database.query allows you to make a dynamic SOQL query at runtime. You can build up a string and then use that as a query string at run time in the database.query statement to make a SOQL call that is determined at run time.

 
database.getQueryLocator returns a Query Locator that runs your selected SOQL query returning list that can be iterated over in bathc apex or used for displaying large sets in VF (allowing things such as pagination). 

 
The query locator can return upto 50 million records and should be used in instances where you want to bactha a high volume of data up (pagination or batch apex). The database.query method should be used in instances where you are wanting to do a dynamic runtime SOQL query for your code.

All Answers

sandeep madhavsandeep madhav
HI,

Hope this helps,

Database.query allows you to make a dynamic SOQL query at runtime. You can build up a string and then use that as a query string at run time in the database.query statement to make a SOQL call that is determined at run time.

 
database.getQueryLocator returns a Query Locator that runs your selected SOQL query returning list that can be iterated over in bathc apex or used for displaying large sets in VF (allowing things such as pagination). 

 
The query locator can return upto 50 million records and should be used in instances where you want to bactha a high volume of data up (pagination or batch apex). The database.query method should be used in instances where you are wanting to do a dynamic runtime SOQL query for your code.
This was selected as the best answer
Daniel Fernando SoaresDaniel Fernando Soares
Thank you very much!!