الأربعاء، 6 سبتمبر 2017

Dynamics 365:How to filter lookup control based on another lookup value using JavaScript

Ex:Display only the contacts that belong to selected account.

//when account lookup value changed
function onAccountChanged() {
    var AccountId = getLookupValue("customerid");
    if (AccountId != null) {

        Xrm.Page.getControl("contactid").removePreSearch(function () {
            addLookupFilter();
        });

        Xrm.Page.getControl("contactid").addPreSearch(function () {
            addLookupFilter();
        });

        Xrm.Page.getAttribute("contactid").setValue(null);
    }

    else {

        Xrm.Page.getControl("contactid").removePreSearch(function () {
            addLookupFilter();
        });

        Xrm.Page.getAttribute("contactid").setValue(null);

    }
}

function addLookupFilter() { 
    var accountId = getLookupValue("customerid"); 

    var fetchXml = "<filter type='and'><condition value='" + accountId + "' attribute='parentcustomerid' uitype='account'  operator='eq' /></filter>";
    Xrm.Page.getControl("alfa_address").addCustomFilter(fetchXml);
}
 //***************************
function getLookupValue(fieldname) {
    var lookupObj = Xrm.Page.getAttribute(fieldname);
    if (lookupObj.getValue() != null)
        return lookupObj.getValue()[0].id;
    else
        return null;
};

ليست هناك تعليقات:

إرسال تعليق