SS2.0 显示消息记录

SS2.0 Display Message on Record

我想使用 SS2.0 和 'new' 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 或任何地方)下运行良好,但尚未找到在 'View' 上加载和执行的方法。这在 2.0 中甚至可能吗?我还必须使用 1.0 技巧吗?

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);


});

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

服务器端:

/**
 *@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
    };
});

基于帮助主题:

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

在视图模式下,您只能附加用户事件脚本(加载前)。

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

改用 N/record 模块。

自 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 
    };
});