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

  • PRANAV

    Member
    January 3, 2017 at 1:23 pm

    Hi Vikas,

    database.query in Salesforce 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. In Batch Apex, if you use Database.query(), it supports 50,000 records only.

     

    database.getQueryLocator in Salesforce 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). In Batch Apex, if you use Database.getQueryLocator(), it supports upto 50 million records.

    Hope this helps you.

    Thanks

  • Vikas Kumar

    Member
    January 4, 2017 at 5:30 am

    Hi Pranav ,

    Thanks for describing the difference between database.query() and database.getQueryLocator()

    • This reply was modified 6 years, 11 months ago by  Forcetalks.
  • shariq

    Member
    September 13, 2018 at 3:05 pm

    Hi,

    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

    Member
    September 13, 2018 at 3:48 pm

    Hi

    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 bypassed but we can retrieve up to 10,000 records.

    Database.query():

    We can retrieve up to 50,000 records.
    If VF page has 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’;
    Database.query(dynQuery);

    Thanks

Log In to reply.

Popular Salesforce Blogs