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
Gaurav KheterpalGaurav Kheterpal 

Getting More than 2000 records in native Android app


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?

Best Answer chosen by Admin (Salesforce Developers) 
bhariharanbhariharan

You can construct a RestRequest in Android by passing in your URL like the snippet below:

 

final RestMethod requestMethod = RestMethod.GET;
RestResponse response = null;
final RestRequest request = new RestRequest(requestMethod, path,
    null, null);
response = client.sendSync(request);

 

'path' represents the endpoint you're trying to hit. You can use client.getAsync() too, if you want to make an async call.

All Answers

chipatoothchipatooth

If you don't mind reading a little objective-c and converting to Android this is how I do it:

 

NSString *nextRecords = [jsonResponse valueForKey:@"nextRecordsUrl"];

SFRestRequest *nextPageOfDataRequest = [[SFRestRequest alloc] init];
nextPageOfDataRequest.endpoint        = @"";
nextPageOfDataRequest.parseResponse   = YES;
nextPageOfDataRequest.path            = nextRecords;
nextPageOfDataRequest.method          = SFRestMethodGET;
[[SFRestAPI sharedInstance] nextPageOfDataRequest delegate:dataFetchedDelegate];

 Essentially just create the Salesforce API RestRequest yourself and set the path to that URL. This way you get the oauth goodness of the API. Not sure if you can do the equivalent of this in Android but I'd be surprised if you couldn't.

Gaurav KheterpalGaurav Kheterpal

Thank you for the suggestion however the problem is that the Android RestClient class does not take a URL as input. Having more than 2,000 records is a common scenario so I'm looking for the right way to do it.

chipatoothchipatooth

It's not the rest client that needs to the url it's RestRequest

 

Does the Android version let you create RestRequest without parameters. ie:

 

RestRequest myRequest = new RestRequest();

myRequest.Path = nextPageUrl;
...

RegistrationActivity.client.sendAsync( myRequest, ...);

?

Pardon my pseudo code that's the rough equivalent of the objective-c code I posted.

bhariharanbhariharan

You can construct a RestRequest in Android by passing in your URL like the snippet below:

 

final RestMethod requestMethod = RestMethod.GET;
RestResponse response = null;
final RestRequest request = new RestRequest(requestMethod, path,
    null, null);
response = client.sendSync(request);

 

'path' represents the endpoint you're trying to hit. You can use client.getAsync() too, if you want to make an async call.

This was selected as the best answer
Gaurav KheterpalGaurav Kheterpal

While this is a stop gap solution, I really do think that this needs to be handled in the getRequestForQuery() method itself by making it capable of accepting large record sets. Thoughts?

Watson CarrenWatson Carren
Download latest mod apk with apktight.com: One Them download Ludo King Mod apk (https://apkright.com/ludo-king-mod-apk/): This app 100% free wit get unlimited money
Watson CarrenWatson Carren
Download latest mod apk with apktight.com: One Them download Ludo King Mod apk (https://apkright.com/ludo-king-mod-apk/): This app 100% free wit get unlimited money
Watson CarrenWatson Carren
Download Personalised wallpapers for android - (https://apkright.com/zedge-premium-apk/) Zedge Premium Apk
Dogi LalDogi Lal
Love music? With Smule (https://apkals.com/smule-mod-apk/), you can sing and make music with friends and fans around the world! Karaoke solo or duet with people across the globe. Download - Smula Mod APK (https://apkals.com/smule-mod-apk/)
James UncleJames Uncle
You can find solution to the same issue I had faced over here: https://apknerd.com/
James UncleJames Uncle
I tried bariharaans answer and it worked like charm. Thanks for asking, find out my solution here: https://instanderapk.com/