Thursday, May 10, 2018

Sample Dynamics 365 CRM- Web API Call

 In the below example, "abc_vehicles" is the Plural name of the Entity data being retrieved, scs_name (Filter has been applied on this)and scs_value(This is the field being retrieved) are fields on this entity.

function sampleWebAPIRetrieve{
var vehName = "Honda";
    var vehValue = "";
   var  orCriteria += "scs_name eq '" + vehName + "'";
    var req = new XMLHttpRequest();
    //Added for single service call start
    var reqString = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/abc_vehicles?$select=scs_values&$filter=" + orCriteria;
    req.open("GET", reqString, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

    req.send();

    var results = JSON.parse(req.response);

    if (results.value.length > 0) {

        for (var i = 0; i < results.value.length; i++) {
            vehValue += results.value[i]["scs_values"] + ";";
        }
    }
}

The query can be easily built using CRM Rest Builder.

Using the $expand, the Fields(except lookup field) can be retrieved from the record referred in the Lookup field.

No comments:

Post a Comment