I'd like to use SS2.0 and the 'new' N/ui/message module to display warnings or errors when a user views a record. In reality, I'd like to understand how to run any 2.0 client script code on record view.
I managed an example I can run, which works from the console:
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
});
}});
Runs fine in edit mode(pageInit or wherever) but haven't found a method to load and execute on 'View'. Is this even possible in 2.0? Do I have to use the 1.0 tricks still?
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.