Real-Time Validation Before Saving a Record (User Event Script – BeforeSubmit)

If we want to prevent users from saving a record under certain conditions, use a BeforeSubmit User Event Script.

Example: Prevent Invoices with Negative Amounts from Being Saved

define(['N/record', 'N/error'], function(record, error) {
    function beforeSubmit(scriptContext) {
        let newRecord = scriptContext.newRecord;
        let amount = newRecord.getValue('total');
        if (amount < 0) {
            throw error.create({ name: 'INVALID_AMOUNT', message: 'Invoices cannot have a negative total amount.', notifyOff: false });
        }
    }
    return { beforeSubmit: beforeSubmit };
});

Leave a comment

Your email address will not be published. Required fields are marked *