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

database.query() and database.getQueryLocator()



What is the Difference between database.query() and database.getQueryLocator()?


(Please give the answer either In Governing limits or in other point of view)





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.


Hope that helps




Can u please tell me some scenarios .........


Where we use batch apex ?

Where we use triggers?



Triggers are run in a particular context whenever a record is inserted, updated, deleted or undeleted and either are run before or after depending upon their type. Typically they are used for complex validation and business logic or data preparation before an object is modified on the database.


Batch apex is an asynchronous processing of a large volume of record 200 at a time. You would use bacth apex when you wanted to run a process that updated a large volume of records and you do not need the result to be instant.


If you are unaware of these sorts of things I would recommend  reading the development guide and documentation available here.




when  using a database.getqueryLocator(),   the  respective vf page search   session is  expiring  after  15 mins if the page is

untouched for 15 mins,  when  replaced  with database.query ()  this is  error is not  replicated,  can  you  please  suggest  why?  and this  fix  is  not recommended  in  my  org

Mark Tyler CrawfordMark Tyler Crawford
You are probably hitting a transaction timeout because the getquerylocator is taking to long to gather all the information.