【问题标题】:D365 Error when calling function from another JS从另一个 JS 调用函数时出现 D365 错误
【发布时间】:2018-06-20 16:28:03
【问题描述】:

我在从另一个 JS 文件调用 JS 函数时遇到问题。

定义函数的主JS。

var Common = Common || {};
Common.BaseAction = Common.BaseAction || {};

Common.BaseAction.SetNotification = function (message, level, uniqueId)
{
Xrm.Page.ui.setFormNotification(message, level, uniqueId);
}

Common.BaseAction.clearNotification = function (uniqueId) {
Xrm.Page.ui.clearFormNotification(uniqueId);
}

我调用函数的地方的JS

var apItem = apItem || {};
apItem.BaseForm = apItem.BaseForm || {};

apItem.BaseForm.SetName = function ()
{
var bookName = Xrm.Page.getAttribute("ap_bookid").getValue()[0].name;
var condition = Xrm.Page.getAttribute("ap_condition").getText();

if (bookName !== null && condition !== null) {
    Xrm.Page.getAttribute("ap_name").setValue(bookName + " - " + condition);
}
}

apItem.BaseForm.CountOverDueBy = function() {
var rentedTill = Xrm.Page.getAttribute("ap_rented_till").getValue();
var nowD = Date.now();

if (rentedTill !== null) {
    var overdueBy = parseInt((Date.now() - rentedTill) / 86400000);

    if (overdueBy > 0) {
        Xrm.Page.getAttribute("ap_overdue_by").setValue(overdueBy);
        Common.BaseAction.SetNotification("Book is Overdue by " + overdueBy 
+ " Days.", "WARNING", "OverDueWarning");
    }
    else {
        Xrm.Page.getAttribute("ap_overdue_by").setValue(null);
        Common.BaseAction.clearNotification("OverDueWarning");
    }
}
}

在实体的表单中,我添加了上述两个文件,common.js 位于顶部,并从事件处理程序中调用函数apItem.BaseForm.CountOverDueBy

Save + Published 和 Ctrl + F5 出现以下错误

ReferenceError: Common is not defined
    at Object.apItem.BaseForm.CountOverDueBy (https://<domain>/%7B636651014350000438%7D/WebResources/ap_ItemFormBase.js?ver=2091450722:24:13)
    at eval (eval at RunHandlerInternal (https://<domain>/form/ClientApiWrapper.aspx?ver=2091450722:153:1), <anonymous>:1:17)
    at RunHandlerInternal (https://<domain>/form/ClientApiWrapper.aspx?ver=2091450722:159:1)
    at RunHandlers (https://<domain>/form/ClientApiWrapper.aspx?ver=2091450722:118:1)
    at OnScriptTagLoaded (https://<domain>/form/ClientApiWrapper.aspx?ver=2091450722:233:1)
    at https://<domain>/form/ClientApiWrapper.aspx?ver=2091450722:202:1

我已经尝试了所有方法,但似乎没有任何效果。

【问题讨论】:

  • 你能把 common.js 作为依赖添加到第二个 js 文件中看看吗?这样您就不需要在每个表单中注册每个文件。
  • 请原谅阿伦,但我该怎么做呢?
  • 转到 js2 网络资源,在依赖选项卡中添加 common.js magnetismsolutions.com/blog/jaredjohnson/2018/02/05/…
  • 是的。谢谢大家的帮助

标签: javascript dynamics-crm crm microsoft-dynamics


【解决方案1】:

在表单中注册 JS 文件的方式,从顶部的 common.js 开始,然后是 ap_ItemFormBase.js 应该可以工作。但是产品团队对脚本文件的性能改进很少,比如延迟脚本加载/并行脚本加载。这有点棘手,现代脚本在 UUI 和 web 等不同客户端之间很混乱。

Like explained in blog post,如果你将前置js设置为依赖,它会在你在依赖文件中消费之前加载。

从解决方案(不是从表单属性)打开ap_ItemFormBase.js 网络资源,转到依赖项 选项卡并添加common.js。这将确保文件在使用参考之前准备就绪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多