According to Netsuite's documentation, User Event Scripts are an appropriate choice for adding custom validation for records. My question is: what is the proper way to handle a record that fails validation in a user event script?
As a more concrete example, I have a user event script that validates vendor bill records in the beforeSubmit
entry point according to criteria unique to our business. The way I'm currently dealing with vendor bills that fail is to throw an error using error.create
in the N\error
module. Here's an example of how this looks:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/error'],
(record, error) => {
const isValid = (newRecord) => {
// Make sure the record is valid...
return false;
};
const invalidRecordError = () => {
const err = error.create({
name: 'INVALID_INVOICE',
message: 'This invoice is not up to snuff.',
notifyOff: true
});
return err;
};
const beforeSubmit = (context) => {
if (!isValid(context.newRecord)) {
// Invalidate record and prevent submit.
throw invalidRecordError();
}
};
return {beforeSubmit}
}
);
This approach does what I want -- records that don't pass validation aren't saved. My only issue is that the error messages aren't displayed in a very useful way to the user. For example, if a record is submitted via CSV import, the error message shown in the results.csv is a JSON version of the error object and includes a stack trace.
So how can I prevent the record from being saved while providing a user-friendly error? Ideally, the error shown to the end-user would just be the message property of the error
object. Is there an alternative to throwing an error that I'm not aware of?