简体   繁体   English

Dynamics CRM 2013 Javascript升级

[英]Dynamics CRM 2013 Javascript Upgrade

We are testing the upgrade of our Dynamics CRM from 2013 to 2016 to understand what customization needs to be upgraded after the CRM upgrade. 我们正在测试从2013年到2016年的Dynamics CRM升级,以了解CRM升级后需要升级的自定义项。

We have the following Javascript webresource that we are using for a particular case. 我们有以下用于特定情况的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");
   }
}

This code is working fine in our Dynamics CRM 2013 and it is intended to do the following: 该代码在我们的Dynamics CRM 2013中运行良好,旨在执行以下操作:

When the Lead form loads, if the value of FIELD1 is set to a specific value, it shows the second field FIELD2 and makes it business required. 加载潜在客户表单时,如果将FIELD1的值设置为特定值,它将显示第二个字段FIELD2并使其成为业务所需。

The tricky part here is that the second field is visible ONLY if the first field contains that specific value when the form loads. 这里最棘手的部分是,仅当表单加载时第一个字段包含该特定值时,第二个字段才可见。 If for some reason the value of the first field during the form load is different, the second field will not show even if you choose that specific value. 如果由于某种原因在表单加载期间第一个字段的值不同,那么即使您选择该特定值,第二个字段也不会显示。

So to resume: 因此,恢复:

  • On Form Load never show FIELD2 在表单加载中从不显示FIELD2

  • If On Form Load FIELD1 value equals X AND FIELD1 value changes -> SHOW FIELD2 and make it required 如果“在窗体上加载”,那么FIELD1的值等于X并且FIELD1的值更改->显示FIELD2并使其为必填项

  • If On Form Load FIELD1 value does not equal X and FIELD1 value changes -> Never show FIELD2 even if the choice of FIELD1 becomes X because it wasn't initially X 如果在“窗体加载”中FIELD1的值不等于X并且FIELD1的值更改->即使FIELD1的选择因为最初不是X而变为X,也从不显示FIELD2

I tried to explain myself so I hope I was clear on what this actual javascript does. 我试图解释自己,所以我希望我对这个实际的javascript的功能很清楚。

The problem where are having after the upgrade is related to the "addEventListener": 升级后出现的问题与“ addEventListener”有关:

TypeError: Cannot read property 'addEventListener' of null at MyOnLoad TypeError:无法在MyOnLoad读取null的属性'addEventListener'

I wanted to ask if someone can help us upgrade this Javascript or if there might be a business rule that can replace this. 我想问问是否有人可以帮助我们升级此Javascript,或者是否存在可以替代它的业务规则。 I actually tried setting up business rules but couldn't separate in the business rule the conditions of load and change. 我实际上尝试设置业务规则,但是无法在业务规则中区分负载和更改的条件。

Thanks 谢谢

Do not access the DOM as you are doing when you use document.getElementById . 使用document.getElementById时,请勿像执行操作那样访问DOM。 In the words of Microsoft : 微软的话来说:

JavaScript developers are used to interacting with Document Object Model (DOM) elements in code. JavaScript开发人员习惯于与代码中的文档对象模型(DOM)元素进行交互。 You might use the window.getElementById method or the jQuery library. 您可以使用window.getElementById方法或jQuery库。 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. 您可以在HTML Web资源中自由使用这些技术,但不支持它们访问Microsoft Dynamics 365应用程序页面或实体表单中的元素。 Instead, access to entity form elements are exposed through the Xrm.Page object model. 而是通过Xrm.Page对象模型公开对实体表单元素的访问。 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. Microsoft Dynamics 365开发团队保留更改页面组成方式(包括元素的ID值)的权利,因此使用Xrm.Page对象模型可以保护您的代码免于实现页面方式的更改。

In your case, you can instead use the supported addOnChange : 在您的情况下,您可以改为使用受支持的addOnChange

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM