• Dogi Lal
  • NEWBIE
  • -1 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

 
Hi all,

We need to implement the following pattern at my org:
  • callout to external data source
  • if that callout takes too long (according to some configurable threshold), log an error (ie do some DML)
  • if that callout timed out on the remote server, try it again
Recognizing the potential for the dreaded "You have uncommitted work pending. Please commit or rollback before calling out." error, I put the error logging code in a future method, thus isolating the DML from the callouts. However, the error is still being thrown. I reduced the issue down to this pattern:
public static void foo() {
    Http http = new Http();
    HttpRequest req = new Httprequest();
    req.setEndpoint('https://test.salesforce.com'); //whatever endpoint
    req.setMethod('GET');
    http.send(req); //works fine
    bar();
    http.send(req); //throws calloutexception
}

@future public static void bar() {

}
Am I correct to assume that calling a future method counts as a DML operation? Is there any documentation I'm missing somewhere?

 


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?