Resetting NetSuite Multi-Select Field Values Does Not Work in beforeLoad

When copying records, you might need to reset fields whose values you do not want to get copied to the new record. It is very common to do this in the beforeLoad event (i.e. server-side):

    // Will not work for multi-select fields; use a client-script instead!
    if (context.type === 'copy') {
        var rec = context.newRecord;
        var fieldId = 'custbody_your_multi_select_field_id';
        if (context.form.getField(fieldId)) {
            rec.setValue(fieldId, ''); // [] or null will also work.

            // Value shows up as cleared in the console
            // but it is still there in the UI and 
            // will get persisted upon saving the record
            log.debug('Weird', rec.getValue(fieldId)); // Expected: []
        }
    }
}

return {
    beforeLoad: beforeLoad
}

})

However, if the field in question is a multi-select field, the value did not get reset! To achieve the desired outcome, We have to switch to a client-side script and reset the field in the pageInit event:

function pageInit(context) {
// Note: Not done in beforeLoad because resetting the value of a multi-select field does not work there!
if (context.mode === 'copy') {
var rec = context.currentRecord;
var fieldId = 'custbody_your_multi_select_field_id';
if (rec.getField(fieldId)) {
rec.setValue(fieldId, ''); // [] or null will also work.
}
}
}

Leave a comment

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