User-friendly error message from user event script

2019-07-28 20:43发布

问题:

I'm attempting to add some custom validation to a record type in NetSuite using SuiteScript 2.0.

On the client side, I've been able to use a client script to validate fields before submit. This works well and shows a user-friendly error message explaining what's wrong.

On the server side, using a user event script, I also perform the same validation. This catches violation from other sources (e.g. CSV upload) which don't use the client script. If a violation is found, the script throws an error using the error module (e.g. throw error.create({...}))

However, there are certain actions the user can perform (e.g. pressing void button on the record's view screen) which don't use the client script yet modify the record. If the user event script detects a violation, it ends up showing an error message (formatted in json) on a blank screen. Not the most user friendly.

At a minimum, is there a way to show a message on the blank screen that isn't formatted in JSON? Ideally, it would be nice to show the error message on the same screen as the button using the message.create/show module.

回答1:

Since the overriding of toString and toJSON functions are not available, i suggest using CSS solution that replaces the original JSON output with the desired text only. Just add your css expression with the message...

Try this:

beforeSubmit: function(scriptContext) {
var errorText = 'This is the error',
    msg = '<style>.text {display: none;}' // this will hide the JSON message
            + '.bglt td:first-child:not(.textboldnolink):after {'
            + 'color:black;font-size:8pt;' // set the desired css for our message
            + 'content: url(/images/5square.gif) \''
            +  errorText 
            + '\'}'                     
        + '</style>',
    err = error.create({
    name: 'NO_JSON',
    message: msg,
    notifyOff: true
});

throw err;
}

You can also remove 'Notice (SuiteScript)' by using:

var msg = '<style>' 
            + '.text {display: none;}' 
            + '.bglt td:first-child:not(.textboldnolink):after {'
            + 'color:red;'
            + 'content: \''
            +  errorText 
            + '\'}' 
            + '.textboldnolink {display: none;}' 
            + '</style>';