How to Use Custom Modules with SuiteScript 2.x in NetSuite
Create and use custom modules with SuiteScript 2.x to enhance code organization and reusability in NetSuite.
TL;DR Opening
Creating and using custom modules with SuiteScript 2.x in NetSuite allows for better code organization, reusability, and modular architecture. This helps streamline development and maintenance of scripts.
What is a Custom Module?
A custom module in SuiteScript 2.x serves as a reusable library of code that can be shared across multiple scripts. By utilizing modules, developers can package functions, manage dependencies, and enhance the maintainability of their code. Here are some core functions you can achieve with custom modules:
- Group Reusable Functions: Collect helper functions into a common library file, improving organization.
- Share with Third Parties: Integrate custom modules into SuiteApps for external sharing.
- Third-party API Import: Easily bring in external APIs that your application may need.
- Separate Utilities from Business Logic: Maintain a clear distinction between business rules and utility functions.
Note: Custom modules are not supported in bundle installation scripts.
Custom Module Prerequisites
Before diving into creating a custom module, familiarize yourself with the following topics:
- [SuiteScript 2.x Script Basics]
- [SuiteScript 2.x Entry Point Script Validation]
- [Module Dependency Paths]
- [Controlling Access to Scripts and Custom Modules]
Creating a Custom Module
When creating a custom module, use the define function. This function facilitates loading other modules and managing dependencies. Your module script must return an object where the typical return value is a function. Here’s the breakdown:
- JSDoc Tags: Use
@NApiVersionto specify the SuiteScript version and@NModuleScopeto define access levels of your module.
Example of a Custom Module Script
1/** 2 * phoneCall.js3 * @NApiVersion 2.x4 * @NModuleScope Public 5 */6 7define (['N/record'], function(record) {8 // Your logic here9});Using Your Custom Module
After creating your custom module, reference it in another script. Follow these steps:
- Create a script to load and run your custom module using the
requirefunction. - Upload both your custom module and entry point script to the NetSuite File Cabinet under SuiteScripts.
- Create a script record and deployment record for the entry point script.
For best practices in naming modules, refer to the section on [Naming a Custom Module].
Callout: Make sure your module and entry point scripts are uploaded correctly to ensure successful execution.
Key Topics for Further Learning
Here are some additional resources to enhance your understanding of SuiteScript 2.x custom modules:
- [Module Dependency Paths]
- [SuiteScript 2.x Script Types]
- [JSDoc Validation in SuiteScript 2.x]
By leveraging custom modules effectively, developers can lead more efficient development processes and create cleaner, modular code that minimizes duplication and encourages reuse.
Key Takeaways
- Custom modules in SuiteScript 2.x enhance code modularity and maintainability.
- Use
defineandrequirefunctions for managing dependencies. - Familiarize yourself with JSDoc for documenting scripts.
- Upload scripts to the NetSuite File Cabinet for effective organization.
- Ensure your scripts are deployed correctly for seamless functionality.
Frequently Asked Questions (4)
Are custom modules supported in bundle installation scripts within NetSuite?
What is required to correctly upload custom modules and entry point scripts in NetSuite?
What function is used to create a custom module in SuiteScript 2.x?
What is the importance of using JSDoc Tags in custom modules?
Was this article helpful?
More in SuiteScript
- Common SuiteScript Errors and Solutions for NetSuite
Common NetSuite script errors include INVALID_SCRIPT_DEPLOYMENT_ID and SSS_AUTHORIZATION_HEADER_NOT_ALLOWED. Learn effective solutions.
- Setting Field Values in SuiteScript for Effective Record
Learn to set field values in SuiteScript effectively, troubleshooting common errors and understanding data types.
- SuiteScript 2.1 Enhancements and API Updates in NetSuite
SuiteScript 2.1 enables execution of 2.0 scripts and supports PATCH method for enhanced API capabilities.
- Enhancements to SuiteScript User Role Context Clarification
SuiteScript updates clarify the user and role contexts for script executions, improving deployment understanding and management.
Advertising
Reach SuiteScript Professionals
Put your product in front of NetSuite experts who work with SuiteScript every day.
Sponsor This Category