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
Surendra nuneSurendra nune 

Difference between StandardSetController(querylocator) & StandardSetController(List<Sobject>)?

Hi,

I am using standard set controller with querylocator for pagination of records. But the problem with using StandardSetController(querylocator) is that if the vf page is idle for 15 minutes and if we try to paginate the records after 15 minutes, am getting the below exception:
Exception

Going through the documentation provided by salesforce, I came to know that query loctors expire every 15mins. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_querymore_querylocator.htm

So, I thought of using other constructor StandardSetController(List<Sobject). What I wanted to understand is, when do we use StandardSetController(querylocator)?  Can any one explain me with a scenario.


 
Best Answer chosen by Surendra nune
Amit Chaudhary 8Amit Chaudhary 8
Some time we need to create the dynamic query in that case we mostly use the StandardSetController(Querylocator) like below code.

            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);

Please let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardsetcontroller.htm

You can instantiate a StandardSetController in either of the following ways:From a list of sObjects:
List<account> accountList = [SELECT Name FROM Account LIMIT 20];
ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(accountList);
From a query locator:
ApexPages.StandardSetController ssc = 
new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name,CloseDate FROM Opportunity]));
The following are constructors for StandardSetController.

StandardSetController(sObjectList)
Creates a new instance of the ApexPages.StandardSetController class for the list of sObjects returned by the query locator.

StandardSetController(controllerSObjects)
Creates a new instance of the ApexPages.StandardSetController class for the specified list of standard or custom objects.
https://developer.salesforce.com/docs/atlas.en-us.198.0.pages.meta/pages/apex_ApexPages_StandardSetController_constructors.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_ApexPages_StandardSetController_methods.htm

Please let us know if this will help you

Thanks
Amit Chaudhary
 
Surendra nuneSurendra nune
Hi Amit,

Thanks for the reply. I have an idea of what are the possible ways to instantiate StandardSetController. My question is when to use StandardSetController(Querylocator) and what is the use of it ? Could please answer this?
Amit Chaudhary 8Amit Chaudhary 8
Some time we need to create the dynamic query in that case we mostly use the StandardSetController(Querylocator) like below code.

            query = 'Select name ,id, phone from Account '+strFilter+ ' limit 1000';
            System.debug('Query ---->'+ query );
            con = new ApexPages.StandardSetController(Database.getQueryLocator(query)); 
            con.setPageSize(2);

Please let us know if this will help you
This was selected as the best answer
Surendra nuneSurendra nune
I see. Thank you.

I tried giving 
con = new ApexPages.StandardSetController(Database.Query(query));

even this works.