Dynamics CRM 2013 Javascript 升级

Dynamics CRM 2013 Javascript Upgrade

我们正在测试从 2013 年到 2016 年的 Dynamics CRM 升级,以了解 CRM 升级后需要升级哪些定制。

我们有以下 Javascript 网络资源用于特定案例。

function MyOnLoad()
{
   var pickListValue = Xrm.Page.getAttribute("field1").getValue();
   if (Xrm.Page.ui.getFormType() == 2 && pickListValue == 100000006)
   {
      var ddlNewField1 = document.getElementById("field1");
      if (ddlNewField1.addEventListener) { 
         ddlNewField1.addEventListener ("change", function () {MyOnChange();}, false);  
      }
      else {
         ddlNewField1.attachEvent('onchange',MyOnChange);
      }
   }
}

function MyOnChange()
{
   if (Xrm.Page.getAttribute("field1").getValue() == "100000006") {    
       Xrm.Page.getControl("field2").setVisible(false);     
       Xrm.Page.getAttribute("field2").setRequiredLevel("none");    
   }

// If the selected value is not Other, hide Specify, and set requirement to Not Required    
   else {    
        Xrm.Page.getControl("field2").setVisible(true);  
        Xrm.Page.getAttribute("field2").setRequiredLevel("required");
   }
}

此代码在我们的 Dynamics CRM 2013 中运行良好,旨在执行以下操作:

加载潜在客户表单时,如果将 FIELD1 的值设置为特定值,它会显示第二个字段 FIELD2 并使其成为业务必需的。

这里棘手的部分是,只有当第一个字段包含表单加载时的特定值时,第二个字段才可见。如果由于某种原因在表单加载期间第一个字段的值不同,即使您选择该特定值,第二个字段也不会显示。

所以要恢复:

我试图解释自己,所以我希望我清楚这个实际的 javascript 是做什么的。

升级后出现的问题与"addEventListener"有关:

TypeError: Cannot read property 'addEventListener' of null at MyOnLoad

我想问一下是否有人可以帮助我们升级这个 Javascript 或者是否有可以替代它的业务规则。我实际上尝试设置业务规则,但无法在业务规则中分离加载和更改的条件。

谢谢

不要像使用 document.getElementById 时那样访问 DOM。用Microsoft的话来说:

JavaScript developers are used to interacting with Document Object Model (DOM) elements in code. You might use the window.getElementById method or the jQuery library. You are free to use these techniques in your HTML web resources, but they are not supported to access elements in Microsoft Dynamics 365 application pages or entity forms. Instead, access to entity form elements are exposed through the Xrm.Page object model. The Microsoft Dynamics 365 development team reserves the right to change how pages are composed, including the ID values for elements, so using the Xrm.Page object model protects your code from changes in how pages are implemented.

对于您的情况,您可以改用支持的 addOnChange

Xrm.Page.getAttribute("field1").addOnChange(MyOnChange)