ByExpandQuery >50 records

Nov 12, 2013 at 12:31 PM
Edited Nov 12, 2013 at 12:32 PM
Hi, I was wondering how do I use the __next attribute to get all records in an expand query?
i.e.
CrmRestKit.ByExpandQuery(entity, columns, expand, filter, async)
    .fail(function (xhr, textStatus, errorThrown) {
        // whatever
        callback.call(null);
    })
    .done(function (data) {
        if (data.d.__next){
            // how do I get more than 50?
        }
        else{
            // I suspect this is in the wrong place
            for (var i = 0; i < data.d.results.length; i++) {
            }
        }
    });
Thanks for your help!

Kuba
Coordinator
Nov 12, 2013 at 8:25 PM
Edited Nov 12, 2013 at 8:40 PM
He Kuba,

take a look at this thread. The ByQueryAll-function provides the functionality you are asking for.

In case you need this for the expand-query, take a look at the implementation for the _byQueryAll function:
///
/// Per default a REST query returns only 50 record. This function will load all records
///
function byQueryAll( entityName, columns, filter, opt_asyn ) {

    var dfdAll = new $.Deferred(),
        allRecords = [];

    byQuery( entityName, columns, filter, opt_asyn ).then( function byQueryAllSuccess( result ) {

        // add the elements to the collection
        allRecords = allRecords.concat( result.d.results );

        if ( result.d.__next ) {

            // the success-handler will be this function
            byQueryUrl( result.d.__next, opt_asyn ).then( byQueryAllSuccess, dfdAll.reject );

            // call the progressCallbacks of the promise
            dfdAll.notify( result );
        }
        else {
            dfdAll.resolve( allRecords );
        }

    }, dfdAll.reject );

    return dfdAll.promise();
}
So the method "byQueryUrl" is the key to load the next chunk of records.

Regards,

Daniel
Coordinator
Nov 19, 2013 at 8:06 AM
Hello Kuba,

any progress on this topic? Did you solved your problem witth the "ByQueryUrl" function?

Daniel
Nov 26, 2013 at 12:46 PM
Hey Daniel,

Apologies for the very tardy response! I've been out the office a lot lately and just catching up. I'm also battling all the must/like to haves which seem to switch every five minutes..

Anyway it's something we'll definitely find useful so I'm going to look into this again this week so I'll let you know if/when I get there!

Kuba
Dec 5, 2013 at 3:16 PM
Hey, finally got around to it - it's pretty much the same as the example above:
function byExpandQueryAll(entityName, columns, filter, opt_asyn) {
        var dfdAll = new $.Deferred(),
                   allRecords = [];

        byExpandQuery(entityName, columns, filter, opt_asyn).
            then(function ByExpandQuerySuccess(result) {
                // add the elements to the collection
                allRecords = allRecords.concat(result.d.results);
                if (result.d.__next) {

                    // the success-handler will be this function
                    byQueryUrl(result.d.__next, opt_asyn).then(ByExpandQuerySuccess, dfdAll.reject);

                    // call the progressCallbacks of the promise
                    dfdAll.notify(result);
                }
                else {
                    dfdAll.resolve(allRecords);
                }
            }, dfdAll.reject);

        return dfdAll.promise();
    }
So to run it
  CrmRestKit.ByExpandQueryAll("EntityName", ["column", "column2"], "rel", filter, false)
            .then(function (data) {
                for (var i = 0; i < data.length; i++) {
                           // whatever
                           var column = data[i]["column"]; 

                }
            });
Marked as answer by thuld on 12/8/2013 at 11:59 PM