Search code examples
javascriptfunctionfetchdynamics-crm

Dynamics 365 - Issue with Jscript/fetchXML: linking entities for filtering a field


The filter I want to create is as follows: I want to filter a field (uneeti_agreementinvoiceproduct) located in my table (msdyn_customerasset). The filter must meet the following conditions: firstly, the customer of the contract must be equal to the asset account. In my fetchXML query from my table (msdyn_agreementinvoiceproduct), I will retrieve the linked contract table (msdyn_agreement) and the field (msdyn_serviceaccount) for comparison. Secondly, I want to display only the products that are already associated with the contract asset. Therefore, I retrieve my product field from my table (msdyn_customerasset) and fetch the (msdyn_product) from my table (msdyn_agreementinvoiceproduct). Here is my retrieval query, along with the error that is occurring. I don't understand this error because the specified field is not associated with the indicated table.

ERROR

function filterProductContratFS(context) {

var formContext = context.getFormContext();

var fieldRecipientCall = formContext.getAttribute("msdyn_account").getValue();
var fieldRecipientCall2 = formContext.getAttribute("msdyn_product").getValue();

if (fieldRecipientCall && fieldRecipientCall2) {
    var accountId = fieldRecipientCall[0].id;
    var productId = fieldRecipientCall2[0].id;

    console.log(accountId);
    console.log(productId);

    var fetchXML =
        "<fetch>" +
        "<entity name='msdyn_agreementinvoiceproduct'>" +
        "<link-entity name='msdyn_agreement' from='msdyn_agreementid' to='msdyn_agreement'>" +
        "<filter>" +
        "<condition attribute='msdyn_serviceaccount' operator='eq' value='" + accountId + "' uitype='account'/>" +
        "</filter>" +
        "</link-entity>" +
        "<link-entity name='msdyn_customerasset' from='msdyn_customerassetid' to='msdyn_agreementinvoiceproductid'>" +
        "<filter>" +
        "<condition attribute='msdyn_product' operator='eq' value='" + productId + "' />" +
        "</filter>" +
        "</link-entity>" +
        "</entity>" +
        "</fetch>";

    var agreementInvoiceProductControl = formContext.getControl("uneeti_agreementinvoiceproduct");
    agreementInvoiceProductControl.addPreSearch(function () {
        agreementInvoiceProductControl.addCustomFilter(fetchXML);
    });

} else {
    return;
}}

Can someone help me resolve this issue?


Solution

  • You should doublecheck on how addCustomFilter function works. It accepts only filter itself. So no entity, not link-entity and so on. The only way I know you might make it works is described here - https://missdynamicscrm.blogspot.com/2014/08/crm-2013-using-addcustomfilter-to-get-filtered-lookup-field-based-on-linked-entity.html