How to retrieve multiple records using FetchXML in Dynamics 365 Online V 9.X using JavaScript WebAPI?

In my previous article, I have explained about How to retrieve multiple records using Odata Query in Dynamics 365 Online V 9.X using JavaScript WebAPI?

And now i will use the same example, which i used in my earlier article mentioned in the above link and retrieve multiple Account records using FetchXML query.

Syntax:

Xrm.WebApi.retrieveMultipleRecords(entityLogicalName, options).then(successCallback, errorCallback);

Under options parameter pass “?fetchXml=+encodeURIComponent(“Insert required FetchXML“)

You can format the FetchXML using FetchXML Formatter Online.

Code: Configure the below code on Onload of Account Form and see the Output.

function RetrieveAllAccountRecords() {
    var accountFetchXML = "<fetch mapping='logical' version='1.0' output-format='xml-platform' distinct='false'>" +
    "  <entity name='account'>" +
    "    <attribute name='name' />" +
    "    <attribute name='primarycontactid' />" +
    "    <attribute name='telephone1' />" +
    "    <attribute name='accountid' />" +
    "    <order descending='false' attribute='name' />" +
    "  </entity>" +
    "</fetch>";

    accountFetchXML = "?fetchXml=" + encodeURIComponent(accountFetchXML);

    var outputText = "Account Name\t\t\tPhone\n---------------------------------------------------\n";
    Xrm.WebApi.retrieveMultipleRecords("account", accountFetchXML).then(
    function success(result) {
        for (var accountRecordsCount = 0; accountRecordsCount < result.entities.length; accountRecordsCount++) {
            outputText += result.entities[accountRecordsCount].name + "\t\t" + result.entities[accountRecordsCount].telephone1 + "\n";
        }
        Xrm.Utility.alertDialog(outputText, null);
    },
    function (error) {
        // Handle error conditions
        Xrm.Utility.alertDialog(error.message, null);
    });
}

Hope you have successfully retrieve multiple records using FetchXML and WebAPI.


Discover more from Arun Potti's Power Platform blog

Subscribe to get the latest posts to your email.

One thought on “How to retrieve multiple records using FetchXML in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Leave a Reply