Chidi Okwudire IT Professional. ERP Enthusiast. NetSuite Certified (Administrator, SuiteCloud Developer II, and ERP Consultant). Celigo Certified (Level 4+). Passionate About Empowerment Through Knowledge Sharing. Always Eager to Learn.

Resetting NetSuite Multi-Select Field Values Does Not Work Before Load!

1 min read

Photo by Glen Carrie on Unsplash

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):

/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

define([], 
function() {
    /**
     * Defines the function definition that is executed before record is loaded.
     * @param {Object} context
     * @param {Record} context.newRecord - New record
     * @param {string} context.type - Trigger type; use values from the context.UserEventType enum
     * @param {Form} context.form - Current form
     * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
     * @since 2015.2
     */
    function beforeLoad(context) {

        // 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, I recently discovered that if the field in question is a multi-select field, the value did not get reset! To achieve the desired outcome, I had to switch to a client-side script and reset the field in the pageInit event:

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define([], 
function() {
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} context
     * @param {Record} context.currentRecord - Current form record
     * @param {string} context.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
     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.
            }
        }
    }

    return {
        pageInit: pageInit
    }
})

This is one of those annoying inconsistencies in the NetSuite APIs. However, now that you know, you won’t have to waste time wondering if you’re losing your mind. All the best.

Related Posts


NetSuite Insights is on a mission to raise the standards around NetSuite practices, one insight at a time. If that resonates with you, learn how you can become an author/collaborator here.

Don’t miss a beat – subscribe to our no-nonsense email list to have these game-changing insights hit your inbox as soon as they’re published. Ignorance? Nah, you’ve got a smarter option. Choose wisdom, choose insights!

Chidi Okwudire IT Professional. ERP Enthusiast. NetSuite Certified (Administrator, SuiteCloud Developer II, and ERP Consultant). Celigo Certified (Level 4+). Passionate About Empowerment Through Knowledge Sharing. Always Eager to Learn.

Leave a Reply

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

×