Dynamics CRM-在父查找为空时删除依赖查找筛选

Dynamics CRM - Remove dependent Lookup Filtering when parent Lookup is blank

本文关键字:查找 删除 依赖 筛选 CRM- Dynamics      更新时间:2023-09-26

在Dynamics CRM 2016*中

我已经使用开箱即用的表单编辑器将"查找"设置为依赖/筛选"父"查找。

因此,当我设置父级查找时,它会根据父级选择过滤子级/依赖项中的选择-如预期/良好。

问题:当我将父级查找设置为空时,筛选将保留并继续根据父级中先前选择的内容来限制依赖的查找选项。我希望它能被删除,儿童查找将不再受到限制。

有JS解决方案吗?我没有添加任何类型的自定义过滤器/视图(因为我使用的是开箱即用的过滤),所以我不确定是否可以删除任何内容来解决这个问题。这是预期的行为吗?

如果开箱即用的依赖查找无法按您想要的方式工作。您可以删除它,并通过JavaScript手动过滤您的查找。如果使用以下代码,则在填充父查找时将筛选子查找。当父查找被清除时,过滤器将从子查找以及中删除

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);
}