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
Ashu sharma 38Ashu sharma 38 

How to retrieve more than 2000 records using resi api

Hi All,

I want to  retrieve more than 2000 records in single response ,as I am using rest Api .
Any solutions ?

/services/data/v46.0/query/?q=Select Id,XXX__c,CompletedDateTime,XXX__c,WhoId  from task WHERE CreatedDate > 2019-10-30T08:03:30.729Z and CreatedDate < 2019-10-30T18:33:30.729Z .

Thanks
 
Jonathan A FoxJonathan A Fox
API returns the results in chunks / batches. You're supposed to call queryMore() function to fetch next "page" of results.
There are lots of questions about it already, including some language-specicic answers (if you're using the libraries for Ruby for example). And there are even better ones at salesforce.stackexchange.com:

https://salesforce.stackexchange.com/questions/35011/get-more-than-2000-with-soap-and-php

Check https://salesforce.stackexchange.com/questions/924/is-there-a-way-to-query-role-hierarchy too

And if you have thousands of records (and if your app can handle bigger chunks!) maybe you should read about bulk API.
Deepali KulshresthaDeepali Kulshrestha
Hi Ashu,

I've gone through your requirement and you can see the below apex code to get more than 2000 record:


1.While OFFSET is limited to a total of 2,000 records, query & queryMore can be configured to return 2,000 records each call.

  To do this, you need to first set the batch size for your connection via:

2.connection.setQueryOptions(2000);

3.Then, you need to call query, and return the first 2,000 records. The return value of query is a QueryResult, which contains the results, as well as a QueryLocator. The QueryLocator can be provided to the queryMore call to essentially perform an identical function as OFFSET, but supports a much higher number of records.

  QueryResult = connection.queryMore(QueryLocator someQueryLocator);

4.From here, you repeat calls to queryMore, using the QueryLocator returned in the QueryResult from each call. Once the value of QueryResult.done is equal to true, you've retrieved all of the results from Salesforce, and can continue doing whatever your java app was doing.

 I've included the example code from the documentation below, which uses query, and queryMore to fetch a large number of records from the database.


Apex Code--->

public void queryRecords() {
   QueryResult qResult = null;
   try {
      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
      qResult = connection.query(soqlQuery);
      boolean done = false;
      if (qResult.getSize() > 0) {
         System.out.println("Logged-in user can see a total of "
            + qResult.getSize() + " contact records.");
         while (!done) {
            SObject[] records = qResult.getRecords();
            for (int i = 0; i < records.length; ++i) {
               Contact con = (Contact) records[i];
               String fName = con.getFirstName();
               String lName = con.getLastName();
               if (fName == null) {
                  System.out.println("Contact " + (i + 1) + ": " + lName);
               } else {
                  System.out.println("Contact " + (i + 1) + ": " + fName
                        + " " + lName);
               }
            }
            if (qResult.isDone()) {
               done = true;
            } else {
               qResult = connection.queryMore(qResult.getQueryLocator());
            }
         }
      } else {
         System.out.println("No records found.");
      }
      System.out.println("\nQuery successfully executed.");
   } catch (ConnectionException ce) {
      ce.printStackTrace();
   }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Fenil Mehta 13Fenil Mehta 13
Hi Deepali,

Thank you for the brief overview....
Have a question : Where can i write connection.setQueryOptions(2000); to set the batch size ?
Can you share whole code snippet so that i can reffer, it will help me to get through if you can ?