+ Start a Discussion
john_mjohn_m 

Mobile hybrid_local Smartsync - how to fetch more than 2000 records using Backbone?

Hi,

I need a few pointers as to how to recursively fetch more than the 2000 record limit imposed by the REST API. I understand how one might do this using purely forcetk (with nextRecordsUrl and the 'done' attribute of the response) but how would I code this using the Backbone extensions?

I've included example code below. Everything works apart from being able to re-query Salesforce (using REST API) to retrieve the additional records.

My stack = Mobile SDK 2.1, Smartsync, Smartstore, Backbone, JQM, RequireJS

// The Contact Model
var Contact = Force.SObject.extend({
    sobjectType: "Contact",
    fieldlist: ["Id", "Name"],
    cache: function() { return App.caches.Contacts;},
    cacheMode: function(method) {
        if (!App.models.offlineTracker.get("isOnline")) {
            return Force.CACHE_MODE.CACHE_ONLY;
        }
        else {
            return (method == "read" ? Force.CACHE_MODE.CACHE_FIRST : Force.CACHE_MODE.SERVER_FIRST);
        }
    }   
});

// The Contact Collection
var ContactCollection = Force.SObjectCollection.extend({
    model: Contact,
    fieldlist: ["Id", "Name"],
    cache: function() { return App.caches.Contacts;}, 
});

// Fetch the records - assume over 2000 contact records
contacts = new ContactCollection();
contacts.config = {type:"soql", query:"SELECT Id, Name FROM Contact"};
contacts.fetch({
    success: function (data) {
     if (data.hasMore()) {
      // how can I use data.getMore()? I get 'RangeError: Maximum call stack size exceeded'
     }                    
    }                   
});

Many Thanks
Ashish_SFDCAshish_SFDC
Hi John, 


The 2000 limit is for Total number of records retrieved by a single SOSL query 2,000

You can get the limits with the below methods, 

Limits.getLimitQueryRows: This will give you the total count of records can be queried in one request.
Limits.getQueryRows: This will give you the total count of records has been queried in one request.

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008oErIAI


You can not do SOQL querie in a "for" loop, Because of SOQL queries execution Governors , change the code into something like this

http://stackoverflow.com/questions/17424718/system-limitexception-too-many-soql-queries-101


Best Practice: Avoid SOQL Queries Inside FOR Loops

http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

http://wiki.developerforce.com/page/Apex_Code_Best_Practices


See the links below for sample code, 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm

http://salesforce.stackexchange.com/questions/348/is-there-a-maximum-value-for-limit-in-a-soql-query


Regards,
Ashish
john_mjohn_m
Hi Ashish,
None of your links are connected to the REST API and the mobile SDK - which is where my query lies. I know why I'm getting the 2000 row limit - I just need to be able to get the next set of rows via the REST API (using Javascript not apex).
If you have experience of using the mobile SDK, Backbone and REST API then please feel free to answer my question.
Have a nice day, John.
akhilesh_sfdcakhilesh_sfdc
Your backbone should look something like this:

// Fetch the records - assume over 2000 contact records
var contacts = new ContactCollection();
contacts.config = {type:"soql", query:"SELECT Id, Name FROM Contact"};

var onFetch = function() {
    if (contacts.hasMore()) {
         contacts.getMore().done(onFetch);
     }
}

contacts.fetch({
    success: onFetch    
});
Kalim SayyadKalim Sayyad
Tried out this solution, but it didn't work out for me. It just ends up with the error
"RangeError: Maximum call stack size exceeded". 
akhilesh_sfdcakhilesh_sfdc
Make sure you are using the latest version of smartsync: https://github.com/forcedotcom/SalesforceMobileSDK-Shared/blob/master/libs/smartsync.js This bug was fixed few months ago: https://github.com/forcedotcom/SalesforceMobileSDK-Shared/commit/a4b987bc405076ee729b1c7be4c7ac6714dc1961 Join the Mobile SDK group at sfdc.co/mobilesdkcommunity
Kalim SayyadKalim Sayyad
Thanks Akhilesh. I used the latest version of smartsync and it solved my problem. Thanks once again.