Validation on the X-Edit Field Changes

Let’s explore the validation and restriction of changes made to specific fields, with a specific focus on the X-Edit context, utilizing a custom User Event Script.

Script Background:

For this discussion, let’s consider a scenario where we need to validate changes made to certain fields in a Discount Tracker record within NetSuite. The fields we’ll focus on include customer, is for all customers, public sector, MPN (Manufacturer Part Number), and HP MPN (Hewlett Packard Manufacturer Part Number). If any of these fields are missing or improperly filled out during a change operation, we’ll throw an error to prevent invalid data submission.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/error', 'N/search'],
    /**
     * @param{error} error
     * @param{search} search
     */
    (error, search) => {
        "use strict";
        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            let record = scriptContext.newRecord;
            let valueCus, mpnvalue, hpmpnvalue, isForAllCustomerValue, publicSectorValue;
            if (scriptContext.type === 'xedit') {
                let columns = ['custrecord_jj_discount_customer', 'custrecord_jj_discount_item_mpn',
                    'custrecord_jj_discount_item_hp_mpn', 'custrecord_jj_discount_all_customer', 'custrecord_jj_discount_public_sector'];
                let fieldslookup = search.lookupFields({
                    type: record.type,
                    id: record.id,
                    columns: columns
                });
                let objFields = record.getFields();
                let fieldChanged = columns.find(value => objFields.includes(value));
                if (fieldChanged) {
                    if (fieldChanged === 'custrecord_jj_discount_customer') {
                        let customerObj = [{ value: record.getValue({ fieldId: fieldChanged }), text: record.getText({ fieldId: fieldChanged }) }];
                        fieldslookup['custrecord_jj_discount_customer'] = customerObj;
                    } else {
                        fieldslookup[fieldChanged] = record.getValue({ fieldId: fieldChanged });
                    }
                }
                valueCus = fieldslookup.custrecord_jj_discount_customer[0]?.value ? true : false;
                mpnvalue = fieldslookup.custrecord_jj_discount_item_mpn ? true : false;
                hpmpnvalue = fieldslookup.custrecord_jj_discount_item_hp_mpn ? true : false;
                isForAllCustomerValue = fieldslookup.custrecord_jj_discount_all_customer;
                publicSectorValue = fieldslookup.custrecord_jj_discount_public_sector;
            } else {
                valueCus = record.getValue({ fieldId: 'custrecord_jj_discount_customer' }) ? true : false;
                mpnvalue = record.getValue({ fieldId: 'custrecord_jj_discount_item_mpn' }) ? true : false;
                hpmpnvalue = record.getValue({ fieldId: 'custrecord_jj_discount_item_hp_mpn' }) ? true : false;
                isForAllCustomerValue = record.getValue({ fieldId: 'custrecord_jj_discount_all_customer' });
                publicSectorValue = record.getValue({ fieldId: 'custrecord_jj_discount_public_sector' });
            }

            //validating
            if ((!valueCus && !isForAllCustomerValue && !publicSectorValue) && (!mpnvalue && !hpmpnvalue)) {
                //if none of the mandatory values exist
                const message = 'Error: The following fields are missing: (i)'Customer' or 'Is For All Customer' or 'Public Sector' (any one of the three fields are to be filled) and (ii) 'MPN' or 'HP MPN' (any one of the two fields are to be filled).';
                throw errorFun(message);
            } else if (!mpnvalue && !hpmpnvalue) {
                //if MPN or HP MPN values dont exist
                const message = 'Error: The following fields are missing: 'MPN' or 'HP MPN'.';
                throw errorFun(message);
            } else if (!valueCus && !isForAllCustomerValue && !publicSectorValue) {
                //if customer or is for all customer or public sector values dont exist
                const message = 'Error: The following fields are missing: 'Customer' or 'Is For All Customer' or 'Public Sector'.';
                throw errorFun(message);
            }
        }

        function errorFun(errMessage) {
            try {
                let errorObj = error.create({
                    message: errMessage,
                    name: 'MANDATORY_FIELD_MISSING',
                    notifyOff: true
                });
                return errorObj.message;
            }
            catch (Err) {
                log.error("Error in the function errorFun", Err);
            }
        }
        return { beforeSubmit }
    });

Leave a comment

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