Writing retrievemultiple request using Web API

In our last post we provided sample code to write retrieve method using Web API. Retrieve methods brings data based on the primary key, so we just need to pass primary key of specific entity record whose data we want to bring and obviously it always returns single record. In this post we are going to provide sample code for retrieve multiple resultset, where we can define query to bring specific data.

Let’s say we want to count number of child contacts records for specific parent account and want to show count on the parent account entity. We do have other options to do this but for our demo purpose we want to do that using java script.

To write our query, we can use $filter with different operators to check any particular condition, for example in our case to count child contact records we need to query contact entity based on the account lookup present in contact entity. In Web API field of the entities are represented as properties, you can check properties for particular entity here. In contact entity type parent customer lookup represented as _accountid_value so we need to compare, current account with this property like below

_accountid_value eq e6e7f801-23b3-4398-8ec2-18518a3f9d3d //where GUID represent parent account
Now to count the resultset, we can use $count in our query so directly we can get number of child record. We can create java script web resource and use below code under text editor:

function retrieveentityCollection(entity, options) {
    var serverURL = Xrm.Page.context.getClientUrl();
    var Query = entity + options;
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if(data['@odata.count']!=null)
                Xrm.Page.getAttribute("numberofemployees").setValue(data['@odata.count']); //we are displaying it in out of the box number o employee field
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send();
}

Now we can write below method to use retrieveentityCollection method

function AccountOnLoad() {
    var Id = Xrm.Page.data.entity.getId().substring(1, 37); //remove braces
    var entity = "contacts";
    //note here we need to use _accounted_value instead of parentcustomerid
    var columnSet = "?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";
  retrieveentityCollection(entity, columnSet);
}

We need to call AccountOnload method on account entity onload like below:
retrievemultipleWebAPI1

Now when we will open account having contacts associated to it, it will show count like below:
retrievemultipleWebAPI2

Stay tuned for more Web API Samples !!

3 thoughts on “Writing retrievemultiple request using Web API

  1. Pingback: Writing retrievemultiple request using Web API - Microsoft Dynamics CRM Community

  2. Pingback: Writing create request using Web API | HIMBAP

  3. Pingback: Writing update request using Web API | HIMBAP

Leave a Reply

Your email address will not be published. Required fields are marked *