Showing posts with label FetchXML Web API. Show all posts
Showing posts with label FetchXML Web API. Show all posts

Tuesday, March 16, 2021

Use FetchXml queries with Web API - JavaScript

1. FetchXML is a proprietary query language that provides capabilities to perform aggregation. 

2. You can pass URL encoded FetchXML as a query to the entity set corresponding to the root entity of the query using the fetchXml query string parameter to return the results from the Web API. 

3. Generate FetchXML queries through Advanced Find.

In the below example, FetchXML is formatted to be used by the Web API service endpoint. You can either do it by storing the FetchXML in a variable and encoding the string with the encodeURI function native to JavaScript or do it directly as shown below. 

Click here  to use FetchXML to query data in the plugin.

var parameter = {};

parameter["new_opportunityid"] = id.toString().replace("{", "").replace("}", "");


var fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
            "<entity name='new_academiccontent'>" +
            "<attribute name='new_academiccontentid' />" +
            "<attribute name='new_name' />" +
            "<attribute name='statecode' />" +
            "<order attribute='new_name' descending='false' />" +
            "<filter type='and'>" +
            "<condition attribute='new_opportunityid' operator='eq' uitype='opportunity' value='{" + parameter["new_opportunityid"] + "}' />" +
            "</filter>" +
            "</entity>" +
            "</fetch>";
 

        var query = "new_academiccontents?fetchXml=" + encodeURI(fetchxml);

Xrm.WebApi.retrieveMultipleRecords("new_academiccontent", query).then( function success(result) { for (var i = 0; i < result.entities.length; i++) { console.log(result.entities[i]); } // perform additional operations on retrieved records }, function (error) { console.log(error.message); // handle error conditions } );



Keep Learning...
Happy CRMing...