Query All code

Developer
Jun 12, 2013 at 11:30 PM
Hey Thuld,

I recently needed to query for all records in CRM and had to create a new function.

Below is the code I used. I followed a similar pattern to your CRMFetchKit.js fetchall function.
function _byQueryAll(entityName, columns, filter, opt_asyn) {
    var dfd = $.Deferred();
    var 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) {
            _byQueryUrl(result.d.__next, opt_asyn).then(_byQueryAllSuccess, dfd.reject);
        }
        else {
            dfd.resolve(allRecords);
        }
    }, dfd.reject);
    return dfd.promise();
}
Hope this helps

Matt
Coordinator
Jun 13, 2013 at 6:43 AM
Hi Matt,

that perfect, thanks for sharing! I will include this in the next release of the CrmRestKit!

Thanks,

Daniel
Coordinator
Jun 13, 2013 at 9:33 AM
Ok, included you method and some other improvments in the new release (2.6.0).

Daniel
Coordinator
Jun 25, 2013 at 8:57 AM
Hi Matt,

in the current version (2.6.1) I extended the "ByQueryAll" a littel bit. We now can register a progrss-handler that is invoked for every chunk of data:
QUnit.asyncTest( 'Retrieve all with progress notification', function () {

    var filter = "Name eq 'Test'",
        ctx = this,
        progressCallCount = 0,
        lastProgressCallArgs = null,
        successHandler = function ( data ) {

            // assert - progress
            QUnit.ok( progressCallCount == 1, 'expected callcount of "1", instead "' + progressCallCount + '"' );
            QUnit.ok( lastProgressCallArgs.d.results.length == 50, 'Expected 50, received "' + lastProgressCallArgs.d.results.length + '"' );

            // assert - success
            QUnit.ok( data.length === ctx.numberOfRecords,
                'Expected the method to load all (' + ctx.numberOfRecords + ') but received ' + data.length );

            // continue with the other tests
            QUnit.start();
        },
        progressHandler = function ( interimData ) {

            progressCallCount++;
            lastProgressCallArgs = interimData;
        };

    // load all data with done-, fail- AND progress-handler
    CrmRestKit.ByQueryAll( 'Account', ['Name'], filter )
        .then( successHandler )
        .fail( onRestError )
        .progress( progressHandler );
} );
Daniel
Marked as answer by thuld on 10/10/2013 at 12:26 AM
Developer
Jun 30, 2013 at 10:29 PM
Hey Daniel,

That looks great. Noticed your roadmap, it's great to see where you're headed. If you need any help let me know :)

Cheers

Matt