【问题标题】:SS2.0 Display Message on RecordSS2.0 显示消息记录
【发布时间】:2016-10-25 17:18:02
【问题描述】:

我想使用 SS2.0 和“新”N/ui/message 模块在用户查看记录时显示警告或错误。实际上,我想了解如何在记录视图上运行任何 2.0 客户端脚本代码。

我管理了一个可以从控制台运行的示例:

require(['N/currentRecord', 'N/ui/message'],
    function(curr, mess) {
        var rec = curr.get();
        var status = rec.getValue('status');
        if (status === 'Unapproved Payment') {
            var myMsg = mess.create({
                title: "PAYMENT ERROR",
                message: status,
                type: mess.Type.ERROR
            }).show({
                duration: 500000
            });
        }});

在编辑模式(pageInit 或任何地方)下运行良好,但没有找到在“视图”上加载和执行的方法。这在 2.0 中甚至可能吗?我还必须使用 1.0 的技巧吗?

【问题讨论】:

  • 我认为在加载视图事件之前您需要一个 SS2。新调用是:form.clientScriptModulePath = './clientScriptPath.js'; NS 示例将路径列为 SuiteScripts/clientScriptPath.js,但我希望我建议的表单有效。否则捆绑或打包(都在他们自己的文件夹中)将被冲洗掉。
  • 我无法使用 clientScriptModule/FileId 正确执行这些代码。完全有可能是用户错误。我最终将上面的示例包装在 jquery 事件触发器中,在

标签: netsuite suitescript


【解决方案1】:

自 2018.2 版本以来,他们在 N/ui/serverWidget 模块中添加了一个名为 Form.addPageInitMessage(options) 的新方法,并且完全符合 OP 的要求。

鉴于上面的示例,它应该看起来像这样。

/**
 *@NApiVersion 2.0
 *@NScriptType UserEventScript
*/
define(['N/ui/serverWidget', 'N/record'],
    function(serverWidget, record) {
        function beforeLoad(context) {

          var rec = context.newRecord;
          var status = rec.getValue('status');
          if (status === 'Unapproved Payment') {
           var myMsg = mess.create({
                title: "PAYMENT ERROR",
                message: status,
                type: mess.Type.ERROR,
                duration: 500000
           });
           context.form.addPageInitMessage({message: myMsg});
        }

    return {
        beforeLoad: beforeLoad 
    };
});

【讨论】:

  • 但这是假设您知道要在 pageInit 上显示消息。如果您想在按下 btn 后显示消息怎么办?例如,单击自定义打印 btn 后会显示提醒消息。我可以想象使用某种类型的复选框 hack 并刷新页面,但这不是我想要的。
【解决方案2】:

这是一个有效的例子。这不好(不是很便携,当然也不是捆绑友好),但它有效:

服务器端:

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/log', 'N/ui/serverWidget'],
    function(record, log, ui) {
        function beforeLoad(context) {
            log.debug({title:'before load with '+ context.type +' on '+ context.form.title});
            if (context.type != 'view') return;
            log.debug({title:'setting client script'});

            var inline = context.form.addField({
                id:'custpage_trigger_it',
                label:'not shown',
                type: ui.FieldType.INLINEHTML,
            });
            inline.defaultValue = "jQuery(function($){ require(['/SuiteScripts/testSS2/testSimpleClient'], function(mod){ console.log('loaded'); mod.showMessage();});});</script>";



            //context.form.clientScriptModulePath = './testSimpleClient.js';
        }


    return {
        beforeLoad: beforeLoad 
    };
});

客户端:

define(['N/ui/message', 'N/currentRecord'], function(msg, currentRecord){
    window.console.log('processing script');
    function showMessage() {
        var rec = currentRecord.get();
        window.console.log('record status is '+ rec.getValue('status'));
        if('Pending Approval' == rec.getValue('status')){
            var myMsg = msg.create({
                title: "Not Committed",
                message: rec.getValue('status'), //'Please Approve',
                type: msg.Type.ERROR
            }).show({
                duration: 10000
            });
        }
    }



    return {
        showMessage:showMessage
    };
});

【讨论】:

  • 同意这并不理想,但它本质上与我最终采用的解决方案相同。 (不出所料,你的比我的更清楚。)再次感谢!
【解决方案3】:

FWIW 以下内容适用于编辑模式,但不适用于查看模式。 (有关截至 2016 年 10 月有效的 hack,请参阅我的另一个答案)我怀疑这将在某个地方得到修复,因为类似的代码在 SS1.0 中已经工作了多年。如果您有业务案例,请向 Netsuite 提交支持案例。

用户事件脚本:

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/log'],
    function(record, log) {
        function beforeLoad(context) {
            log.debug({title:'before load with '+ context.type +' on '+ context.form.title});
            //if (context.type != 'view') return;
            log.debug({title:'setting client script'});
            context.form.clientScriptModulePath = './testSimpleClient.js'; //relative to the user event script file
        }

    return {
        beforeLoad: beforeLoad 
    };
});

testSimpleClient 是:

define(['N/ui/message', 'N/currentRecord'], function(msg, currentRecord){
    window.console.log('processing script');
    function showMessage(rec) {
        window.console.log('record status is '+ rec.getValue('status'));
        //if('Pending Approval' == rec.getValue('status')){
            var myMsg = msg.create({
                title: "PAYMENT ERROR",
                message: rec.getValue('status'), //'Please Approve',
                type: msg.Type.ERROR
            }).show({
                duration: 100000
            });
        //}
    }

    setTimeout(function(){
        showMessage(currentRecord.get());
    }, 1500);


});

【讨论】:

    【解决方案4】:

    基于帮助主题:

    A current record instance can be accessed via the following ways:
     - The context object that gets passed into the client script entry point.
    

    在查看模式下,您只能附加一个用户事件脚本(beforeLoad)。

    N/currentRecord 模块仅在客户端脚本上运行,这就是它不起作用的原因。

    改用 N/record 模块。

    【讨论】:

    • 实际上带有 N/currentRecord 的代码在客户端工作得很好。查看工作示例
    • 是的。基本上,N/currentRecord 用于客户端,N/record 用于服务器端。
    猜你喜欢
    • 2021-11-11
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多