0
votes

My fetch works fine when I use just attributes, but when I add link-entity, or filter it doesn't work. Here is the link code. Please help to find my mistake!

These two entities are connected with the name field in Invoice and invoice field in invoiceline.

    var fetchInvoices = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">';
        fetchInvoices += '<entity name="hms_invoice">';
        fetchInvoices += '<attribute name="hms_name"/>';
        fetchInvoices += '<attribute name="hms_customer" />';
        fetchInvoices += '<link-entity name="hms_invoiceline" from="hms_invoice" to="hms_name">';
        fetchInvoices += '<attribute name="hms_amount" />';
        fetchInvoices += '</link-entity>';
        fetchInvoices += '</entity>';
        fetchInvoices += '</fetch>';

    var invoices = XrmServiceToolkit.Soap.Fetch(fetchInvoices);
    alert(invoices.length);

I need to get name and customer from Invoice, and amount from invoice lines.

1

1 Answers

0
votes

I would refrain form using XrmServiceToolkit.Soap.Fetch as you will have to add third party library for using it and SOAP is unsupported in latest version, Instead I would advice using Webapi as this is the supported way and simplest way fetching data.

Here is the link which will provide you more details w.r.t Webserevice calls in D365 crm.

Below I have tried fetching All contacts for a particular Account Id, For example: Account A has 3 contacts linked to it and I am fetching all the 3 contacts.

Here is the sample code for it.

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts?$select=_accountid_value,contactid,fullname&$filter=_accountid_value eq 4930FC98-5F75-E911-A83C-000D3A385DD4", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var _accountid_value = results.value[i]["_accountid_value"];
                var _accountid_value_formatted = results.value[i]["[email protected]"];
                var _accountid_value_lookuplogicalname = results.value[i]["[email protected]"];
                var contactid = results.value[i]["contactid"];
                var fullname = results.value[i]["fullname"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

There is one more out of box new way of calling webapi with smaller code as below

Xrm.WebApi.online.retrieveMultipleRecords("contact", "?$select=_accountid_value,contactid,fullname&$filter=_accountid_value eq 4930FC98-5F75-E911-A83C-000D3A385DD4").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
            var _accountid_value = results.entities[i]["_accountid_value"];
            var _accountid_value_formatted = results.entities[i]["[email protected]"];
            var _accountid_value_lookuplogicalname = results.entities[i]["[email protected]"];
            var contactid = results.entities[i]["contactid"];
            var fullname = results.entities[i]["fullname"];
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);