NetSuite Custom Modules Using SuiteScript
Using NetSuite SuiteScript 2.x, you can create custom modules, including third-party, AMD, and non-AMD modules. This facilitates the organization of code in a modular and well-structured manner, making it easier to read and modify. Additionally, it empowers you to develop and access extra, tailored API functionality.
Create a personalized module to achieve the following:
- Consolidate frequently used functions into a shared library file, allowing for the arrangement of supporting code, like helper functions and customized APIs, into reusable sections. These custom modules are then loaded in your main script.
- Incorporate custom modules into SuiteApps and make them accessible to external parties. Also, import third-party APIs.
- Systematically organize and segregate utilities from business logic.
Creating a Custom Module
To forge your custom module, employ the define Object. This object is indispensable for both custom module and entry point script creation. Additionally, when referencing a script file in the File Cabinet via a relative path, it is advisable to utilize the define Object consistently.
Each script for a custom module must define an object that gets returned when the script’s entry point is triggered. While this object could be a static value like a string, it’s more typical for custom module scripts to return a function. Similar to an entry point function in a standard entry point script, this function accepts a context object as its argument.
In your custom module scripts, it’s possible to incorporate both standard and custom JSDoc tags. Utilize the @NApiVersion and @NModuleScope tags to specify the script’s API version and module scope. In the case of custom modules that are not designated as entry point scripts, the @NApiVersion tag defines the SuiteScript version your module utilizes. It serves to prevent compatibility issues if a script referencing this custom module employs a different SuiteScript version with language features or syntax not supported by your module. The @NModuleScope tag dictates whether other scripts and accounts can access your custom module.
Using Your Custom Module:
To utilize your custom module, it needs to be referenced by another deployed script. You are required to generate a distinct entry point script designed to reference and activate your custom module. By utilizing the require function, you can load and specify the name of your custom module through an absolute path.
Once you’ve crafted your custom module and the referencing script, the next step involves uploading them to your File Cabinet in NetSuite (Navigate to Documents > Files > File Cabinet > SuiteScripts). In the case of entry point scripts that make use of your custom module, it’s essential to create both a script record and a deployment record.