Sample code Vendor Subsidiary Update using copy Record

/**

 * @NApiVersion 2.1

 * @NScriptType MapReduceScript

 */

define([‘N/record’, ‘N/search’, ‘N/log’], (record, search, log) =>{

    /**

     * Retrieves all vendor records in old subsidiaries.

     * This function is part of the input stage and is used to return the vendor records that meet specific criteria.

     *

     * @param {Object} inputContext – The context object for the input stage.

     * @returns {search.Search} – A search object containing vendor records to be processed in the Map/Reduce script.

     */

    const getInputData = (inputContext) => {

      try{

      let oldSubsidiaries = [‘OLD_SUBSIDIARY_ID1’, ‘OLD_SUBSIDIARY_ID2’]; // Replace with actual IDs

      return search.create({

        type: “vendor”,

        filters:

        [

           [“subsidiary”,“noneof”,“24”],“AND”,

           [“isinactive”, “is”, “F”],

           

        ],

        columns:

        [

           search.createColumn({name: “internalid”, label: “Internal ID”}),

           search.createColumn({name: “companyname”, label: “Company Name”}),

           search.createColumn({name: “subsidiary”, label: “Primary Subsidiary”}),

           search.createColumn({name: “entityid”, label: “ID”})

        ]

     });

    }

    catch(e){

      log.error(‘Error in getInputData’, e);

    }

    }

 

     /**

     * Processes each vendor record and prepares data for the reduce stage.

     * This function is part of the map stage and extracts relevant data from each vendor record.

     *

     * @param {Object} mapContext – The context object for the map stage, containing key-value pairs for each record.

     */

    const map = (mapContext) => {

      try{

      let searchResult = JSON.parse(mapContext.value);

      let vendorId = searchResult.id

      log.debug(‘vendorId’,vendorId)

      let vendorName = searchResult.values.entityid;

      log.debug(‘searchResult’,searchResult)

     

        mapContext.write({

          key : vendorId,

          value : JSON.stringify({

              entityId: searchResult.values.entityid,

              vendorName: searchResult.values.companyname,

          })

      });

 

   

    }

    catch(e){

      log.error(‘Error in map’, e);

      }

    }

 

   /**

     * Updates vendor records by changing their subsidiary and creating a copy with new attributes.

     * This function is part of the reduce stage and handles each vendor by updating the existing one and creating a modified copy.

     *

     * @param {Object} reduceContext – The context object for the reduce stage, containing keys and aggregated values for each group.

     */

    const reduce = (reduceContext) => {

      try {

        log.debug(‘reduceContext’,reduceContext)

        let vendorId = reduceContext.key;

        // Parse values passed from the map stage

        let vendorData = JSON.parse(reduceContext.values[0]);

        let entityId = vendorData.entityId;

        let vendorName = vendorData.vendorName;

        log.debug(‘vendorId’,vendorId)

        log.debug(‘vendorData’,vendorData)

        log.debug(“Entity ID”, entityId);

        log.debug(“Vendor Name”, vendorName);

        let newEntityId = entityId + ‘-old’;

        let newVendorName = vendorName + ‘-old’;

        record.submitFields({

          type: record.Type.VENDOR,

          id: vendorId,

          values: {

              entityid: newEntityId,

              companyname: newVendorName,

          }

      });

        let copiedVendor = record.copy({

            type: “vendor”,

            id: vendorId

        });

       

        copiedVendor.setValue({ fieldId: ‘entityid’, value: entityId || “” });

        copiedVendor.setValue({ fieldId: ‘custentity_jj_old_suppiler’, value: vendorId });

        copiedVendor.setValue({ fieldId: ‘companyname’, value: vendorName || “” });

        // copiedVendor.setValue({ fieldId: ‘taxitem’, value:””});

        copiedVendor.setValue({ fieldId: ‘subsidiary’, value: 24  });

        let subsidiaryCount = copiedVendor.getLineCount({ sublistId: ‘submachine’ });

        log.debug(‘subsidiaryCount’,subsidiaryCount) ;

       

       

        for (let i = subsidiaryCount1 ; i >= 0 ; i) {

            copiedVendor.removeLine({ sublistId: ‘submachine’, line: i });

        }

        log.debug(‘Removed Subsidaries’)

        let newVendorId = copiedVendor.save({

          enableSourcing: true,

          ignoreMandatoryFields: true

        });

        log.debug(‘Copied Vendor New Vendor ID:’, newVendorId);

       

       

        record.submitFields({

          type: record.Type.VENDOR,

          id: vendorId,

          values: {

              custentity_jj_new_suppiler: newVendorId,

              isinactive:true

             

          }

      });

      log.debug(‘Exit’)

   

       

       

    } catch (err) {

        log.error(‘Error @reduce’, err);

    }

    }

 

    /**

     * Summarizes the results of the Map/Reduce script.

     * This function is part of the summarize stage, providing information on errors, processed records, and usage statistics.

     *

     * @param {Object} summaryContext – The context object for the summarize stage, containing details about the script execution.

     */

    const summarize = (summaryContext) => {

   

    }

 

    return {

      getInputData: getInputData,

      map: map,

      reduce: reduce,

      summarize: summarize

    };

  });

 

Leave a comment

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