Reputation: 5576
In Dynamics CRM 2016*
I've set a Lookup to be dependent/filtered on a "parent" Lookup, using the out-of-the-box form editor.
Thus when I set the parent Lookup, it filters the choices in the child/dependent by the parent selection - as expected/good.
Problem: when I set the parent Lookup to blank, the filtering remains and continues to limit the dependent Lookup choices by what was previously selected in the parent. I hoped it would be removed and that the child Lookup would no longer be restrained.
Is there JS solution? I'm not adding a custom filter/view of any kind (since I'm using the out-of-the-box Filtering), so I'm not sure if I can remove anything to fix this. Is this the expected behavior?
Upvotes: 0
Views: 3136
Reputation: 619
If the out-of-the-box dependant lookup does not work the way you want. You can remove it and filter your lookup manually through JavaScript. If you use below code your child lookup will be filtered when the parent lookup is filled. When the parent lookup is cleared the filter will be removed from the child lookup as well
function OnChange_ParentLookup() {
// Manually add pre Search event
// Check if parent lookup is emptied or filled.
if (Xrm.Page.getAttribute("parentLookup").getValue() != null) {
// Remove the previous filter if changing the parent lookup to another value without clearing it first.
Xrm.Page.getControl("childLookup").removePreSearch(addCustomFilterToChildLookup);
Xrm.Page.getControl("childLookup").addPreSearch(addCustomFilterToChildLookup);
}
else {
Xrm.Page.getControl("parentLookup").removePreSearch(addCustomFilterToChildLookup);
}
}
function addCustomFilterToChildLookup() {
// Check if parent lookup is not empty.
// Use value in parent lookup to filter child lookup
var parentLookup = Xrm.Page.getAttribute("parentLookup").getValue();
if (parentLookup == null || parentLookup.length <= 0) return;
// attribute = the field on the child entity that references the parent entity
// uitype = entity name of parent lookup
// value = GUID of the parent lookup
var childLookupFilter = "<filter type='and'><condition attribute='parentLookup' operator='eq' uitype='parentLookupEntityName' value='" + parentLookup[0].id + "' /></filter>";
Xrm.Page.getControl("childLookup").addCustomFilter(childLookupFilter);
}
Upvotes: 2