Activity Forums Salesforce® Discussions Difference between database.query() and database.getQueryLocator() in Salesforce?


    February 12, 2018 at 11:58 am

    Hi Ankit,

    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 batch apex or used for displaying large sets in VF (allowing things such as pagination).

    Hope this helps you.

  • shariq

    September 13, 2018 at 3:04 pm


    1. Database.query() retrives 50K records where as Database.getQueryLocator() retrives 50 million records from Database.
    2. Database.query() is used to construct dynamic query instead of using SOQL query where as Database.getQueryLocator() is the return type of start method of a Batch class.

    Hope this helps!

  • Parul

    September 13, 2018 at 3:47 pm


    Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. If you use a querylocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassedbut we can retreive up to 10,000 records.


    We can retrieve up to 50,000 records.
    If VF page have read only attribute, use Database.getQueryLocator().
    In Batch Apex, if we use Database.query(), it supports 50,000 records only.

    String fieldName = 'Name,Phone';
    String dynQuery = 'select Id ' + fieldName + ' From Account';



Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos