Retrieve Multiple Dynamics CRM Records with XrmServiceToolkit

In order to install XrmServiceToolkit check this blog post.

In this library, I found RetrieveMultiple function really helpful and I use it for different of purposes like to create a custom numbering system in CRM records, for instance since you can retrieve multiple record, you can loop through all your record and get all the fields in those.

here is the signature of the function :

     retrieveMultipleRecords = function (type, options, successCallback, errorCallback, onComplete, async)

One important thing is that successCallback only returns one page of records (50) at the time so you should loop through the results and push the records into an array outside of the function.

Here are Some Sample retrieveMultipleRecords calls:

1. This sample retrieves all accounts with all fields in the account form and push them in accountArray :

      var accountArray = new Array();

        XrmServiceToolkit.Rest.RetrieveMultiple(
                     "AccountSet",
                     "",// if you leave it empty, it retrieves all fields
                     function (results) {
                         if (results.length >= 1)
                             for (var i = 0; i < results.length; i++) {
                                 accountArray.push(results[i]);
                             }
                     },
                     function (error) {
                         alert(error.message);
                     },
                     function onComplete() {
                         //alert(" records should have been retrieved.");
                     },
                     false
                 );

     alert(accountArray.length); //this should show the number of account records in CRM

2. This sample retrieves only AccountID and AcountNumber when “new_AccountingApproved”(custom field) check box is checked:

   var accountArray = new Array();

        XrmServiceToolkit.Rest.RetrieveMultiple(
                     "AccountSet",
                     "$select=AccountId, AccountNumber &$filter=new_AccountingApproved eq true",
                     function (results) {
                         if (results.length >= 1)
                           //accountArray = results;  //don't do this! instead use push to array. for instance if you have 58 accounts in CRM, accountArray will only get 8 of them, because successCallback will run twice.
                             for (var i = 0; i < results.length; i++) {
                                 accountArray.push(results[i]);
                             }
                     },
                     function (error) {
                         alert(error.message);
                     },
                     function onComplete() {
                         //alert(" records should have been retrieved.");
                     },
                     false
                 );

In case you are interested ,Here is RetrieveMultipleRecords function definition from Source code of the project:

var retrieveMultipleRecords = function (type, options, successCallback, errorCallback, onComplete, async) {
        ///<summary>
        /// Sends synchronous/asynchronous request to retrieve records.
        ///</summary>
        ///<param name="type" type="String">
        /// The Schema Name of the Entity type record to retrieve.
        /// For an Account record, use "Account"
        ///</param>
        stringParameterCheck(type, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the type parameter is a string.");
        ///<param name="options" type="String">
        /// A String representing the OData System Query Options to control the data returned
        ///</param>
        if (options != null)
            stringParameterCheck(options, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the options parameter is a string.");
        ///<param name="successCallback" type="Function">
        /// The function that will be passed through and be called for each page of records returned.
        /// Each page is 50 records. If you expect that more than one page of records will be returned,
        /// this function should loop through the results and push the records into an array outside of the function.
        /// Use the OnComplete event handler to know when all the records have been processed.
        /// </param>
        callbackParameterCheck(successCallback, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the successCallback parameter is a function.");
        ///<param name="errorCallback" type="Function">
        /// The function that will be passed through and be called by a failed response.
        /// This function must accept an Error object as a parameter.
        /// </param>
        callbackParameterCheck(errorCallback, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the errorCallback parameter is a function.");
        ///<param name="OnComplete" type="Function">
        /// The function that will be called when all the requested records have been returned.
        /// No parameters are passed to this function.
        /// </param>
        callbackParameterCheck(onComplete, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the OnComplete parameter is a function.");
        ///<param name="async" type="Boolean">
        /// A Boolean representing if the method should run asynchronously or synchronously
        /// true means asynchronously. false means synchronously
        /// </param>
        booleanParameterCheck(async, "XrmServiceToolkit.REST.retrieveMultipleRecords requires the async parameter is a boolean.");