
FetchXML will be very useful to use with Web API for complex queries as I have encountered and issue where we can’t make grouping conditions using the normal ODATA style filter, Also a lot of operators provided by fetchXML is not provided by the normal ODATA Queries example: In , Today , Next X Days, etc.
So to call fetchXML using JavaScript and XRM.WebApi please follow these simple steps:
Construct your fetchXML using Advanced find and then download FetchXML.
var fetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
'<entity name="account">' +
'<attribute name="accountid" />' +
'<order attribute="fullname" descending="false" />' +
'<filter type="and">' +
'<condition attribute="statecode" operator="eq" value="0" />' +
'</filter>' +
'</entity>' +
'</fetch>';
Prefix you fetchXML variable with the word “?fetchXml” and the concatenate the encodeURIComponent(fetchXML)
fetchXML = "?fetchXml="+encodeURIComponent(fetchXML);
Call retrieveMultipleRecords as below:
Xrm.WebApi.online.retrieveMultipleRecords("account", fetchXML).then(
function success(results) {
if (results.entities.length > 0) {
var accountid = results.entities[i]["accountid"];
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
}
);
Hope it is useful!