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.

Advertisement

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.