问题描述
我们尝试上传一个小的 JS 网络资源来过滤特定的查找字段,但我一直收到错误:ReferenceError: Web 资源方法不存在:preFilterLookup
这是我们尝试使用的代码:
function preFilterLookup() {
Xrm.Page.getControl("new_opportunitytypelookup").addPreSearch(function () {
addLookupFilter();
});
}
function addLookupFilter() {
var oppScope = Xrm.Page.getAttribute("new_opportunityscope").getText();
if (oppScope.getText()=="BMS Operational Outsourcing") {
fetchXml = "<filter type="and">
<condition attribute="cr2f5_opportunitytypeid" operator="in">
<value uiname="aaS Offering" uitype="cr2f5_opportunitytype">{42403355-925B-EB11-A812-000D3A8C6500}</value>
<value uiname="Operational Outsourcing" uitype="cr2f5_opportunitytype">{DF7CC32A-925B-EB11-A812-000D3A8C6500}</value>
</condition>
</filter>";
Xrm.Page.getControl("new_opportunitytypelookup").addCustomFilter(fetchXml);
}
}
我最初认为我们甚至还为我们的特定领域设置了正确的“On Change”事件设置,方法定义为 Opportunity Scope - On Change Event
解决方法
首先你应该有一个关于表单加载事件的方法,并定义 addPreSearch() 以在其中进行查找。
此外,Xrm.Page 在 Dynamics 365 中已弃用,因此您应该改用 formContext。
所以我们得到了这个方法,它应该在加载表单时触发:
function onLoad(executionContext){
var formContext = executionContext.getFormContext();
formContext.getControl("new_opportunitytypelookup").addPreSearch(function () {
addLookupFilter(executionContext);
});
}
剩下的代码:
function addLookupFilter(executionContext) {
var formContext = executionContext.getFormContext();
var oppScope = formContext.getAttribute("new_opportunityscope").getText();
if (oppScope == "BMS Operational Outsourcing") {
var fetchXml = "
<filter type="and">
<condition attribute="cr2f5_opportunitytypeid" operator="in">
<value uiname="aaS Offering" uitype="cr2f5_opportunitytype">{42403355-925B-EB11-A812-000D3A8C6500}</value>
<value uiname="Operational Outsourcing" uitype="cr2f5_opportunitytype">{DF7CC32A-925B-EB11-A812-000D3A8C6500}</value>
</condition>
</filter>";
formContext.getControl("new_opportunitytypelookup").addCustomFilter(fetchXml);
}
}
同样不要忘记在注册 onLoad 事件时传递执行上下文。