Auto-populating values in Item records.

The client needs to auto-populate two field values while creating the item record.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record','N/search'],
    /**
 * @param{record} record
 */
    (record,search) => {

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

        }

        /**
         * Retrieves brand details based on the provided brand ID.
         * @param {number} brand - The ID of the brand to retrieve details for.
         * @returns {boolean} - Returns true if brand details are found, false otherwise.
         */
        function getBrandDetails(brand) {
            try {
                let customrecord_cseg_aha_brandSearchObj = search.create({
                    type: "customrecord_cseg_aha_brand",
                    filters:
                    [
                       ["internalid","anyof",brand], 
                       "AND", 
                       ["custrecord_aha_luxury","is","T"]
                    ],
                    columns:
                    [
                       search.createColumn({name: "name", label: "Name"}),
                       search.createColumn({name: "custrecord_aha_luxury", label: "Luxury"})
                    ]
                 });
                 let searchResultCount = customrecord_cseg_aha_brandSearchObj.runPaged().count;
                 log.debug("customrecord_cseg_aha_brandSearchObj result count",searchResultCount);
                 if(searchResultCount){
                    return true
                 }else{
                     return false;
                 }
            } catch (error) {
                log.error('error@etBrandDetails', error);
                return false;
            }
        }
                

        /**
         * 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) => {
            // script needs to work in create context only
           
        }

        /**
         * Defines the function definition that is executed after 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 afterSubmit = (scriptContext) => {
            if (scriptContext.type !== 'create') return;
            let recordId = scriptContext.newRecord.id;
            let itemRecord = record.load({type: record.Type.INVENTORY_ITEM, id: recordId, isDynamic: false});
            let defaultAppliance =  itemRecord.getValue({fieldId: 'custitem_jj_default_appliance'});
            let brand =  itemRecord.getValue({fieldId: 'cseg_aha_brand'});
            let productType= itemRecord.getValue({fieldId: 'custitem_jj_product_type'});
            if(defaultAppliance || getBrandDetails(brand)){
                itemRecord.setValue({fieldId: 'custitem_jj_web_map_policy', value: true}); 
            }
            if(productType == '2' || productType == '3'){
                itemRecord.setValue({fieldId: 'custitem62', value: true});
            }
            itemRecord.save({enableSourcing: false, ignoreMandatoryFields: true});
        }

        return {beforeLoad, beforeSubmit, afterSubmit}

    });

Leave a comment

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