• : 1-877-596-8218
  • : sales@aecc.ca
# # #
#

Include Custom Module in NetSuite 2.0 Script.

The challenge:
In SuiteScript 1.0, we can put the common use or shared functions into a script file and share it with multiple Script Record by defining this file as the Script Library Files.
This increases the code reusability when writing the script, especially when designing a large project with multiple scripts working together.
But in SuiteScript 2.0, how can we achieve this?

SuiteScript 2.0 is designed to be modular, using the AMD style. SuiteScript 2.0 has several advantages compared to SuiteScript 1.0:

  • More Natural and Object-Oriented Syntax
  • Modularity
  • Efficiency
  • New Map/Reduce Script Type

It is always recommended to use 2.0 API instead of 1.0 API to write the SuiteScript.

The Solution:

For this problem, we can define the custom module and include it in the 2.0 SuiteScript. The custom module can be included into multiple SuiteScript, to achieve the same functionality as the Library File in SuiteScript 1.0.

This starts with writing a custom module, in the following example, the function which checks if the input variable is empty will be put into the custom module, another User Event Script loads this custom module and use the function to check empty variable.


define(function(){
    /**
     * Return true if the input variable is empty
     * @param stValue
     * @returns {boolean}
     */
    function isEmpty ( stValue ) {
        if ((stValue == '') || (stValue == null) || (stValue == undefined)) {
            return true;
        }
        else {
            if (stValue instanceof String) {
                if ((stValue == '')) {
                    return true;
                }
            }
            else if (stValue instanceof Array) {
                if (stValue.length == 0) {
                    return true;
                }
            }

            return false;
        }
    }
    return {
        isEmpty: isEmpty
    }
});

Then upload this script file into the NetSuite file Cabinet SuiteScript folder.
In the SuiteScript, add this custom module as shown below:


define(['N/record','./Util_module.js'],function (record,tool) {
    function beforeSubmit ( context ){
        if(tool.isEmpty(context.newRecord.getValue({fieldID: 'memo'}))){
            log.debug("Memo field is empty", "Will set to Test");
            context.newRecord.setValue({fieldId:"memo", value: "Test"});
        }
    }
    return {
        beforeSubmit: beforeSubmit
    }
});

The User Event Script above now is able to use the isEmpty() function from Util_module.js file defined at the beginning.