Validation script for CSV import

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************************************************************************
 * 
 * Grand Appliance-US-NS
 *
 * GRAUN-12 | Validation script for avoiding Duplication of Custom records


*********************************************************************************************************************
*
* Author: Jobin & Jismi
* 
* Date Created : 26-July-2024


* Created By: Linu Paul, Jobin and Jismi IT Services
* 
* Description : The validation on the custom record for duplicate prevention.
*
* REVISION HISTORY
* 
**************************************************************************************************************************/
define(['N/search', 'N/runtime'],
    /**
 * @param{search} search
 */
    (search, runtime) => {
        "use strict"
        /**
        * function to find the duplicate of the zip code record
        * @param {string} zipCode 
        * @returns {string}
        */
        function isDupicateZipCode(zipCode) {
            let zipCodeId = '';
            try {
                let customrecord_jj_zip_codes_ftusn_8SearchObj = search.create({
                    type: "customrecord_jj_zipcode",
                    filters:
                        [
                            ["custrecord_jj_zip_code", "is", zipCode]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal Id" }),
                        ]
                });
                customrecord_jj_zip_codes_ftusn_8SearchObj.run().each(function (result) {
                    zipCodeId = result.getValue({ name: "internalid", label: "Internal Id" });
                });
                return zipCodeId;
            } catch (e) {
                log.error('error@isDupicateZipCode', e);
                return zipCodeId;
            }
        }


        /**
         * function to find the duplicate of the route record
         * @param {string} route 
         * @returns {string}
         */
        function isDupicateRoute(route) {
            let routeId = ''
            try {
                let customrecord_jj_routes_ftusn_10SearchObj = search.create({
                    type: "customrecord_jj_routes",
                    filters:
                        [
                            ["name", "is", route]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal Id" })
                        ]
                });
                customrecord_jj_routes_ftusn_10SearchObj.run().each(function (result) {
                    routeId = result.getValue({ name: "internalid", label: "Internal Id" });
                });
                return routeId;
            } catch (e) {
                log.error('error@isDupicateRoute', e);
                return routeId;
            }
        }


        /**
         * function to find the duplicate of the shared route record
         * @param {string} sharedRoute 
         * @returns {string}
         */
        function isDupicateSharedRoute(sharedRoute) {
            let sharedRouteId = ''
            try {
                let customrecord_jj_shared_route_codeSearchObj = search.create({
                    type: "customrecord_jj_shared_route_code",
                    filters:
                        [
                            ["name", "is", sharedRoute]
                        ],
                    columns:
                        [
                            search.createColumn({ name: "internalid", label: "Internal Id" })
                        ]
                });
                customrecord_jj_shared_route_codeSearchObj.run().each(function (result) {
                    sharedRouteId = result.getValue({ name: "internalid", label: "Internal Id" });
                });
                return sharedRouteId;
            } catch (e) {
                log.error('error@isDupicateSharedRoute', e);
                return sharedRouteId;
            }
        }
        /**
         * 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) => {
            if ((scriptContext.type == 'create' || scriptContext.type == 'edit') && (runtime.executionContext !== runtime.ContextType.USER_INTERFACE)) {
                let recObj = scriptContext.newRecord;
                if (recObj.type == 'customrecord_jj_zipcode') {
                    let zipCode = recObj.getValue({ fieldId: 'custrecord_jj_zip_code' })
                    let zipCodeId = isDupicateZipCode(zipCode);
                    if ((scriptContext.type == 'edit' && zipCodeId && zipCodeId != recObj.id) || (scriptContext.type == 'create' && zipCodeId)) {
                        recObj.setValue({ fieldId: 'custrecord_jj_zip_code', value: '', ignoreFieldChange: true, })
                        throw 'The Zip Code already exists!';
                    }
                }
                if (recObj.type == 'customrecord_jj_routes') {
                    let route = recObj.getValue({ fieldId: 'name' })
                    let routeId = isDupicateRoute(route);
                    if ((scriptContext.type == 'edit' && routeId && routeId != recObj.id) || (!recObj.id && routeId)) {
                        recObj.setValue({ fieldId: 'name', value: '', ignoreFieldChange: true, })
                        throw 'The Route already exists!';
                    }
                }
                if (recObj.type == 'customrecord_jj_shared_route_code') {
                    let sharedRoute = recObj.getValue({ fieldId: 'name' })
                    let sharedRouteId = isDupicateSharedRoute(sharedRoute);
                    if ((scriptContext.type == 'edit' && sharedRouteId && sharedRouteId != recObj.id) || (scriptContext.type == 'create' && sharedRouteId)) {
                        recObj.setValue({ fieldId: 'name', value: '', ignoreFieldChange: true, })
                        throw 'The Shared Route already exists!';
                    }
                }
            }
        }


        return { beforeSubmit }


    });


Leave a comment

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