locked
How do you sort a retrievemultiple request using javascript? RRS feed

  • Question

  • I am making amendments to someone elses retrieve multiple query, but how do I sort the retrieved data?

    retrieveMultiple("new_contactSet", "substringof('" + currentRec + "',new_GUID)", SearchCompleted, null);

    I want to sort the data by "new_sequence"  and it should be ascending.

    Anyone know how?

    Thursday, May 1, 2014 2:34 PM

All replies

  • You need to look inside the retrieveMultiple method (sure it is defined inside one of the libraries you are referencing) and check how the retrive is performed (probably using SOAP) 

    Here an example of a SOAP call using the orderexpression tag

    http://xrmpalmer.wordpress.com/2013/07/11/crm2011-retrievemultiple-soap-order-format/


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Thursday, May 1, 2014 3:04 PM
  • Hi Guido, I've found that it is a sample provided by Microsoft:

    function retrieveMultiple(odataSetName, filter, successCallback, errorCallback) {
     /// <summary>
     /// Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
     ///     Retrieve multiple records
     /// </summary>
     /// <param name="odataSetName" type="string" required="true">
     /// 1: set -    a string representing an OData Set. OData provides uri access
     ///                 to any CRM entity collection. examples: AccountSet, ContactSet,
     ///                 OpportunitySet. 
     /// </param>
     /// <param name="filter" type="string">
     /// 1: filter - a string representing the filter that is appended to the odatasetname
     ///                 of the OData URI.     
     /// </param>
     /// <param name="successCallback" type="function" >
     /// 1: callback-a function that can be supplied as a callback upon success
     ///                 of the ajax invocation.
     /// </param>
     /// <param name="errorCallback" type="function" >
     /// 1: callback-a function that can be supplied as a callback upon error
     ///                 of the ajax invocation.
     /// </param>
     //odataSetName is required, i.e. "AccountSet"
     if (!odataSetName) {
      alert("odataSetName is required.");
      return;
     }
     else
     { odataSetName = encodeURIComponent(odataSetName); }

     //Build the URI
     var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName;

     //If a filter is supplied, append it to the OData URI
     if (filter) {
      odataUri += "?$filter=" + encodeURIComponent(filter);
     }

    The problem is, I am having difficulty working out where to add: orderby=new_sequence asc

    Thursday, May 1, 2014 3:24 PM
  • Hi,

    If you use the SDK.REST library that MS provides in the SDK's sample code, which is the general recommendation, you can use the retrieveMultipleRecords function with the following signature:

    retrieveMultipleRecords (type, options, successCallback, errorCallback, OnComplete)

    The second parameter is your full oData query which can include the sort parameter as well as the $select and $filter.

    E.g.

    retrieveMultipleRecords("contact","$select=firstname,lastname&$filter=last name eq 'Nicholson'&$orderby=firstname asc",successCallbackFunction(data), errorCallbackFunction(e), onCompleteFunction());

     

    http://msdn.microsoft.com/en-us/library/gg985387.aspx

    If you want to continue using the above code you can just concatenate the order by parameter to the odataUri variable:

    odataUri += "&$orderby=firstname asc";

    Cheers,

    A.

    Thursday, May 1, 2014 8:27 PM