Allocating Subsidiaries and Currencies

Jira Code: TRS:575

We can allocate all the subsidiaries and currencies to a customer or vendor after get created. This will ensure that all the active subsidiaries (children) and currency will allocate to all of the customers and vendors.
This is achieved using a User Event script which will trigger after the record creation. Basically the script will allocate all the active subsidiaries and currencies to the newly created entity record except the base subsidiary and currency which it already has.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************************************
 * TRUST BRIDGE GLOBAL
 * 
 * UE SCRIPT TO BULK UPDATE ALL SUBSIDIARIES AND CURRENCIES FOR CREATED DONORS AND CHARITIES FROM 
 * CURRENCY/SUBSIDIARY TRIGGER
 * 
 * TRS-575
 ******************************************************************************************************
* DATE: 17/12/2019
* 
* AUTHOR: JOBIN AND JISMI IT SERVICES LLP
* MIDHUN NATH

  REVISON HISTORY
*
* 
* 
* 
******************************************************************************/
/** Define global variables here*/


/*********************************************************************************/

define(['N/record', 'N/search', 'N/ui/serverWidget', 'N/url', 'N/runtime', 'N/task'],

    function(record, search, serverWidget, url, runtime, task) {

        /*****************************************************************************************
        Apply try and catch
        *****************************************************************************************/
        function applyTryCatch(DATA_OBJ, NAME) {
            function tryCatch(myfunction, key) {
                return function() {
                    try {
                        return myfunction.apply(this, arguments);
                    } catch (e) {
                        log.error("error in " + key, e);
                        return false;
                    }
                };
            }
            for (var key in DATA_OBJ) {
                if (typeof DATA_OBJ[key] === "function") {
                    DATA_OBJ[key] = tryCatch(DATA_OBJ[key], NAME + "." + key);
                }
            }
        }
        /*****************************************************************************************
        To check whether a value exists in parameter
        *****************************************************************************************/
        function checkForParameter(parameter, parameterName) {
            if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter != " " && parameter !== 'false') {
                return true;
            } else {
                if (parameterName)
                    log.debug('Empty Value found', 'Empty Value for parameter ' + parameterName);
                return false;
            }
        }

        /*****************************************************************************************
        Return main
        *****************************************************************************************/
        return {

            /*****************************************************************************************
             * Function definition to be triggered before record is loaded.
             *
             * @param {Object} scriptContext
             * @param {Record} scriptContext.newRecord - New record
             * @param {Record} scriptContext.oldRecord - Old record
             * @param {string} scriptContext.type - Trigger type
             * @Since 2015.2
             *
             *****************************************************************************************/
            afterSubmit: function(scriptContext) {

                try {

                    var currentEvent = scriptContext.type; //event type
                    var currentRecordType = scriptContext.newRecord.type; //record type
                    var currentRecordId = scriptContext.newRecord.id; //record if

                    //verify record type
                    if (currentRecordType == 'currency' || currentRecordType == 'subsidiary') {

                        //verify event type
                        if (currentEvent == 'create') {

                            //Mas reduce
                            var mrTask = task.create({ taskType: task.TaskType.MAP_REDUCE, scriptId: 'customscript_trs575jjmr_bulkupdatecurrsu', deploymentId: 'customdeploy_trs575jjmr_bulkupdatecurrsu', params: {} });
                            var mrTaskId = mrTask.submit();

                        }

                    }

                } catch (e) {
                    log.debug("Err@afterSubmit", e);
                    log.error("Err@afterSubmit", e);
                }

            }
        }


    });

Leave a comment

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