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:
connection.setQueryOptions(2000);
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);
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.
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();
}
}
OFFSET
, if you checked the documentation, the max value forOFFSET
is 2k. Have you tried using other methods, such asqueryMore
?queryMore
as an answer. It is probably the best option for paging through multiple records from the Tooling API.