Validate FetchXml by using ValidateFetchXmlExpression WebAPI function in the script

var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'+

'<entity name="account">'+

'<attribute name="name" />'+

'<attribute name="testfield" />'+

'<attribute name="primarycontactid" />'+

'<attribute name="accountid" />'+

'<order attribute="name" descending="false" />'+

'</entity>'+

'</fetch>';

//variable declartion

var requestUrl = "";

var serverUrl = Xrm.Utility.getGlobalContext().getClientUrl();

//generate Web API request

requestUrl += serverUrl + "/api/data/v9.2/ValidateFetchXmlExpression(FetchXml=@p1)?@p1='"+ fetchXml + "'";

//create ajax request

var jqxhr = $.ajax({

type: "GET",

contentType: "application/json; charset=utf-8",

datatype: "json",

async: false,

url: requestUrl,

beforeSend: function (xhr) {

//Specifying this header ensures that the results will be returned as JSON.

xhr.setRequestHeader("Accept", "application/json");

xhr.setRequestHeader("Content-Type", "application/json; odata.metadata=minimal");

xhr.setRequestHeader("OData-MaxVersion", "4.0");

xhr.setRequestHeader("OData-Version", "4.0");

xhr.setRequestHeader("Prefer", "odata.include-annotations=*");

}

});

//check response of request

if (jqxhr.responseJSON != undefined) {

//get respose in object format

var response = { valid: jqxhr.statusText, data: jqxhr.responseJSON };

//check for error

if (jqxhr.responseJSON.error != undefined) {

alert(jqxhr.responseJSON.error.message);

} else {

//get actual result with message

var result = response.data;

}

}

Conclusion:

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store