Custom Module Development Using SuiteScript 2.x
Learn to create custom modules in SuiteScript 2.x for reusable functions, API integration, and better code organization.
Creating a custom module with SuiteScript 2.x allows developers to build modular, reusable code that enhances functionality and promotes better organization. Custom modules can encapsulate logic and functions to be shared across scripts, making development more efficient and manageable.
What Are Custom Modules?
Custom modules are specialized scripts that can group reusable functions into library files. By encapsulating functionalities within a module, developers can keep their codebase clean and modular, aiding in maintenance and readability. Here are some primary uses for custom modules:
- Reusable Functions: Create libraries that group common helper functions or custom APIs, which can easily be invoked from entry point scripts.
- Integration with SuiteApps: Custom modules can be included in SuiteApps, enabling developers to distribute enhanced functionality to third parties.
- Third-Party APIs: Import and integrate third-party API functionalities directly into your SuiteScripts.
- Separation of Logic: Distinguish between utilities (helper functions) and business logic, which can improve code clarity.
Note: Custom modules cannot be utilized in bundle installation scripts.
Prerequisites for Creating Custom Modules
Before diving into custom module development, familiarize yourself with the following topics:
- SuiteScript 2.x Script Basics
- Entry Point Script Validation
- Global Objects in SuiteScript 2.x
- Module Dependency Paths
- Controlling Access
Steps to Create a Custom Module
Creating a custom module requires the use of the define function which is a standard practice for all SuiteScript modules. Here's a brief overview:
- Define the Module: Use
defineto specify your module's dependencies and the associated callback function. - Return an Object: Each module should return an object. This might typically be a function that gets executed in the context of the script that calls it.
- JSDoc Tags: Utilize JSDoc tags like
@NApiVersionand@NModuleScopeto clarify the API version in use and control access to your module. For detailed information on formatting, refer to SuiteScript 2.x JSDoc Validation.
Code Sample: Basic Custom Module Structure
Here’s an example of a basic custom module:
1/** 2 * myModule.js 3 * @NApiVersion 2.x 4 * @NModuleScope Public 5 */6 7define(['N/record'], function(record) {8 function createRecord(type, properties) {9 var newRecord = record.create({10 type: type,11 isDynamic: true12 });13 // Set record properties14 for (var prop in properties) {15 newRecord.setValue({16 fieldId: prop,17 value: properties[prop]18 });19 }20 return newRecord.save();21 }22 return {23 createRecord: createRecord24 };25});Using Your Custom Module
To utilize your newly created module, create another script to reference it:
- Reference Using the Require Function: Load your custom module using an absolute path with the
requirefunction. - Deploy the Scripts: Both the custom module and the entry point script must be uploaded to the NetSuite File Cabinet under SuiteScripts.
- Create Script and Deployment Records: For any entry point referencing your module, ensure to create the necessary script records and deployment records.
For a better understanding of the custom module naming conventions and best practices, review the related tutorials and documentation entries.
Key Takeaways
- Custom modules enhance code organization and reusability in SuiteScript 2.x.
- Use the
definemethod to create and manage module dependencies effectively. - Always implement JSDoc tags to facilitate compatibility and access control.
- Custom modules allow for better separation of business logic from utility functions.
Frequently Asked Questions (4)
Can custom modules be used in bundle installation scripts?
What are the prerequisites to start developing custom modules in SuiteScript 2.x?
How do you define a custom module in SuiteScript 2.x?
What steps must be taken after creating a custom module to ensure it's used properly?
Was this article helpful?
More in SuiteScript
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- Binary File Support in N/https Module for SuiteScript
SuiteScript enhances capabilities with binary file support in the N/https module, allowing improved data handling in external communications.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
