【问题标题】:TypeError: Cannot read property 'getValue' of null at welcomeAlertTypeError:无法在 WelcomeAlert 处读取 null 的属性“getValue”
【发布时间】:2019-02-21 20:46:09
【问题描述】:

我是 CRM 的新手。 我只需要在页面的 OnLoad 事件上,显示一条 JavaScript 警报消息:“欢迎 'Account Name'”。 这是我的简单代码:

function welcomeAlert()
{
  var userName = Xrm.Page.getAttribute("name").getValue();
  if(userName !== null)
  {
    alert("Welcome " + userName + "!");
  }
}

但我收到错误消息 onLoad: TypeError: Cannot read property 'getValue' of null at welcomeAlert。

如果我的代码看起来像下面的代码一切正常。

function welcomeAlert()
{
    alert("Welcome ");
}

有人可以帮忙吗?也许属性名称不正确。但我不知道如何检查。

【问题讨论】:

  • 所以 getAttribute('name') 显然没有返回任何东西。由于我不知道 Xrm 或 Page 是什么,我无法提供更具体的帮助。打开调试器并在 var username 行放置一个断点并将鼠标悬停在对象上以查看它们具有哪些属性。这可能会有所帮助。
  • 您的主页中是否存在“名称”字段?

标签: javascript dynamics-crm


【解决方案1】:
  1. 验证属性name 以确保其在表单中
  2. 如果该字段是自定义属性,那么它将具有发布者名称前缀 ex。 new_name
  3. 如果该字段被添加到页眉/页脚部分或 BPF 阶段,那么它将被重命名为 header_name
  4. 检查它是否在表单中多次隐藏或添加并使用浏览器开发人员工具栏检查 DOM
  5. 您可以在访问getValue() 之前像if(formContext.getAttribute("name") != null 一样进行验证

【讨论】:

    【解决方案2】:

    根据您的 CRM 版本,Xrm.Page 可能已被弃用。参考deprecation page

    执行此操作的正确方法是使用executionContext 对象,这是一个可以由 CRM 传递给您的方法的参数。 Microsoft 提供了一个示例来说明如何做到这一点here,但步骤如下:

    1. 更新您的方法以包含一个新参数:例如function welcomeAlert(executionContext)
    2. 使用executionContext 检索formContext:例如var formContext = executionContext.getFormContext()
    3. 使用formContext 代替Xrm.Page:例如var userName = formContext.getAttribute("name").getValue();
    4. 当您在 CRM 中注册此函数时,您需要确保选中 Pass execution context as first parameter 选项,否则 CRM 不会将 executionContext 传递给您的函数

    综合起来,你的方法应该是这样的:

    function welcomeAlert(executionContext)
    {
      var formContext = executionContext.getFormContext();
      var userName = formContext.getAttribute("name").getValue();
      if(userName !== null)
      {
        alert("Welcome " + userName + "!");
      }
    }
    

    【讨论】:

    • 我做了所有 4 个阶段,但这并没有解决问题。得到同样的错误
    • 表单上有name 字段吗?
    • 没有。我在 docs.microsoft 上读到了这个,所以我认为 Account Name 的属性是“name”。 var nameValue = Xrm.Page.getAttribute("name").getValue();将 Account Name 字段的值分配给 nameValue 变量。 Account Name 的正确属性是“parentcustomerid”。这段代码对我有用。 function welcomeAlert() { var userName = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].name;如果(用户名!== null){警报(“欢迎”+用户名+“!”); } } 感谢您的回复。
    • 哦,您不在 Account 表单上 - 您在 Contact 表单上。现在这是有道理的。干得好自己修复它:)
    【解决方案3】:

    我解决了我的问题! 我在 docs.microsoft 上读到了这个,所以我认为 Account Name 的属性是“name”。 var nameValue = Xrm.Page.getAttribute("name").getValue(); 将 Account Name 字段的值分配给 nameValue 变量。

    帐户名称的正确属性是“parentcustomerid”。这段代码对我有用。

        function welcomeAlert() 
         { 
           var userName = Xrm.Page.getAttribute("parentcustomerid").getValue()[0].name; 
           if(userName !== null) 
           { 
             alert("Welcome " + userName + "!"); 
           } 
    
         }
    

    谢谢大家的回复。

    【讨论】:

    • 天啊,完全不同的属性。很高兴你把它整理出来。由于您是该网站的新手,因此在发布您的问题/问题时,请提供一条有用的建议 - 尝试添加有关您正在谈论的内容的屏幕截图..以便轻松识别问题。祝你有美好的一天:)
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 2022-01-12
    • 2021-12-04
    • 2021-12-17
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多