1

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.

angrycrab
  • 830
  • 1
  • 9
  • 23
  • There's a solution provided here: http://stackoverflow.com/questions/38632350/how-to-show-the-custom-error-message-without-stack-trace-using-suitescript-2-0-i?rq=1 however, it doesn't appear to work as it just prints out a longer json error message. – angrycrab Dec 23 '16 at 03:19
  • I have not found a way to do this in 2.0; you just always get the JSON message. In 1.0, if you throw an `nlobjError` using `nlapiCreateError`, you get a nice clean NetSuite page with just the error's message in plain text. – erictgrubaugh Dec 23 '16 at 16:00
  • There's a new solution provided here: http://stackoverflow.com/questions/38632350/how-to-show-the-custom-error-message-without-stack-trace-using-suitescript-2-0-i?rq=1 Looks like all you need to do is throw a string instead of the error object? – angrycrab Dec 23 '16 at 16:29
  • Sort of works - it does return just the string as the error message. However, since you don't specify notifyOff on the thrown object, the suite script will email the script owner. You have to turn off exception notification on the suitescript to avoid this. Can't believe there's not better support for reporting exceptions to the user from a user event script. – angrycrab Dec 23 '16 at 23:29
  • 1
    The solution provided by @Koby Pichkhadze below works fine, and it resolve both the Notification problem and the JSON problem. – B. Assem Jun 27 '18 at 09:28

1 Answers1

2

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>';
B. Assem
  • 1,058
  • 9
  • 12
  • 1
    Please add an explanation to your answer. – Stav Alfi May 10 '18 at 22:12
  • 2
    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... – Koby Pichkhadze May 10 '18 at 23:36
  • Was this deprecated? I'm running this and it is not giving me a pretty error display. It's just text json – Godrules500 Oct 28 '21 at 14:17
  • @Godrules500 that's exactly what I was going to mention when I saw the classes, NetSuite probably updated them, this is not really reliable and if you want to show a good message for your validations you should do them in a client script – Matias Feb 14 '22 at 13:20