Getting More Than 2000 Records in Salesforce Native Android App Using RestRequest.getRequestForQuery()

I’m in a tricky situation where my org has more than 2,000 records for objects which I’m trying to fetch in my Android native app

RestRequest restRequest = RestRequest.getRequestForQuery( getString(R.string.api_version), soql);

and then I get the response using

RegistrationActivity.client.sendAsync(restRequest, new AsyncRequestCallback() {
@Override
  public void onSuccess(RestRequest request, RestResponse result) {
}

This approach suffers the limitation that it can only return 2,000 records in the SOQL. In the response, I’m getting the nextRecordsUrl in the response and I can retrieve it as

nextRecordsUrl =result.asJSONObject().getString("nextRecordsUrl").toString();

Now, my understanding is that I will need to fire HTTP GET requests to this URL using this approach

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint("http://na1.salesforce.com" + nextRecordsUrl);
req.setMethod('GET');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
HttpResponse res = h.send(req);

and then parse the response. While this approach is theoretically possible, I’m not very keen on mixing 2 approaches – getting the initial records using RestClient getRequestForQuery() method and follow up data (more than 2,000 records) using the HttpRequest method.

Can anyone guide me on the correct/ coherent way of fetching object data (more than 2,000 records) in a native force.com Android app?

Answer

The RestRequest object (which you initially construct one of with the getRequestForQuery method), has a generic constructor which you can use for the queryMore case.

RestRequest queryMore = new RestRequest(RestMethod.GET, nextRecordsUrl, null);

You can then use the RestRequest object the exact same way as before by passing it to sendAsync on your RestClient instance.

Attribution
Source : Link , Question Author : Gaurav Kheterpal , Answer Author : superfell

Leave a Comment