SS2.0 Display Message on Record

别说谁变了你拦得住时间么 提交于 2019-11-29 18:07:16

so a working example. This is not good (not very portable and certainly not bundle friendly) but it works:

Server Side:

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

Client Side:

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

FWIW the following works in edit mode but not view mode. (See my other answer for a hack that works as of October 2016) I suspect this'll be fixed somewhere along the way since similar code has worked for years in SS1.0. If you have a business case please file a support case with Netsuite.

user event script:

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

with testSimpleClient being:

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


});

Based on help topic:

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

In view mode, you can only attach a user event script (beforeLoad).

N/currentRecord module only runs on client-side scripts which is why it didn't work.

Use N/record module instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!