将模块加载到从自定义按钮调用的客户端脚本中

Loading modules into Client script called from a custom button

我在 javascript AMD 模块中有一些代码。如果我创建一个引用它的 ClientScript 并部署它,则代码在从 ClientScript 事件调用时运行良好。但是,如果我从自定义按钮单击事件中调用完全相同的函数,它会在 require.js 中出现意外错误(请参阅控制台输出的 post 末尾)。

这是 ClientScript 代码:

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope public
 */

'use strict'
require.config({
    paths: {
        'iceUtil': 'SuiteScripts/ieCommon/iceUtil'
    }
});

define(['N/log', 'iceUtil'], function (https, iceUtil) {
    function calculateConfigurations() {
        console.log('calculateConfigurations: before native log call');
        log.debug({
            title: 'calculateConfigurations',
            details: 'nothing'
        })

        console.log('calculateConfigurations: before iceUtil log call');
        iceUtil.logDebug('calculateConfigurations - iceUtil log', 
            'nothing');
    }

    function fieldChanged() {
        iceUtil.logDebug('pageInit', 'begining client event');
        calculateConfigurations();
        iceUtil.logDebug('pageInit', 'ending client event');
    }

    return {
        calculateConfigurations: calculateConfigurations
        , fieldChanged: fieldChanged
    }
});

iceUtil.js 实际上只是包装了本机 NetSuite 日志功能。

这工作正常,并且在从 fieldChanged 事件触发调用时显示所有日志。

但是如果我使用以下代码将它连接到自定义按钮,则只有第一个 console.log('recordId: ' + recordId); 调用会运行。第一次调用iceUtil.logDebug炸弹。

这是连接按钮的代码:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(["N/log", "../ieCommon/iceUtil"], function (log, iceUtil) {

    function addCalcConfigsButton(context) {
        if(context.type==context.UserEventType.EDIT) {
            context.form.clientScriptFileId = 6222;
            //context.form.clientScriptModulePath = 'SuiteScripts/ieOppMetrics/clientCalculateConfigurations.js'; // This also works
            context.form.addButton(
                {
                    id : 'custpage_buttonid',
                    label : 'Calculate Configurations',
                    functionName : 'calculateConfigurations'
                }
            );
            iceUtil.logDebug("addCalcConfigButton - iceUtil log", 
                "Button added.");

            log.debug({
                title: 'addCalcConfigButton - native log',
                details: 'Button added.'
            });
        } 
    }

    return {
        beforeLoad : addCalcConfigsButton
    };
});

这是从 fieldChanged 事件调用并运行时此代码的控制台输出(日志消息也显示在 NS 执行日志中):

[Log] calculateConfigurations: before native log call
[Log] calculateConfigurations: before iceUtil log call

这是按下按钮失败后的控制台输出(NS 执行日志中没有日志消息):

[Log] calculateConfigurations: before native log call
[Error] UNEXPECTED_ERROR: Unexpected Error
    onError (bootstrap.js:150)
    onError (NsRequire.js:645)
    check (NsRequire.js:994)
    enable (NsRequire.js:1251)
    init (NsRequire.js:882)
    (anonymous function) (NsRequire.js:1547)

"The first call to iceUtil.logDebug bombs." 这非常模糊。 Post 实际的错误输出(最重要的是主要错误和引用您自己的代码的堆栈跟踪中的第一行)给人们一些提示来查找您的错误的代码。

即使没有实际错误,我也会说这一行看起来非常可疑:

functionName : 'calculateConfigurations(' + context.newRecord.id + ')'

我不知道处理您对 context.form.addButton 的调用的底层代码是什么,但我怀疑 属性 名称 functionName 是唯一有效的值是一个 [String] 包含 context.

范围内的函数名称

相反,您正在非常努力地创建某种可能对您调用的 addButton() 方法没有任何意义的动态函数创建。

context.form.addButton从哪里来的?它不是本机 js,并且从您的代码示例中没有明显的参考。该方法是由您未提及的库提供的吗?

addButtonfunctionName 属性 只需要函数的 name。我不相信你可以像那样调用函数并传递参数。

您的点击处理程序需要从 context 或使用 NetSuite 的 N/currentRecord 模块读取当前记录的 ID。

由于 NetSuite 中要求对象配置的更新,您可能遇到了问题。打出来会有点多,所以请参阅 NetSuite 文档 here 了解详细信息。如果这不能为您的情况提供足够的上下文,请参阅 2017.2 发行说明第 67 和 68 页。希望这有帮助。

事实证明这根本不是模块加载问题;当从不同的地方调用时(NS 客户端事件与来自自定义按钮的任意 JS 函数),这是 N/log 代码和 JS 环境中的一个问题。

当从按钮调用 log.debug() 时,JS 环境定义了以下变量并具有值: NLScriptIdForLoggingNLDeploymentIdForLogging,但当从 NS 客户端事件成功调用 log.debug() 时,这些未定义。

如果您在调用 log.debug() 之前 运行 以下内容,它将 运行 从按钮代码成功:

delete NLScriptIdForLogging;
delete NLDeploymentIdForLogging;

我不确定这些尝试做什么,所以我只是检查它们并跳过 log.debug() 调用(或者将它路由到 JS 控制台,如果它在那里的话)。

这似乎是一个 NS 错误。有谁知道我该如何举报?