Custom Modules in SuiteScript 2.x for Modular Code

Custom modules in SuiteScript 2.x enhance code organization and reusability, allowing easy integrations and API use.

·3 min read·View Oracle Docs

TL;DR Opening

Custom modules in SuiteScript 2.x enable developers to create reusable, organized code structures for improved efficiency and collaboration. They facilitate better modularization, making it easier to manage complex API integrations.

What Are Custom Modules?

Custom modules in SuiteScript 2.x allow developers to group reusable functions into cohesive files. This organization leads to cleaner code, which simplifies updates and enhances readability. Key benefits of using custom modules include:

  • Reusable Functions: Group functions relevant to specific features or tasks, facilitating reuse across scripts.
  • Sharing SuiteApps: Distribute custom modules as part of SuiteApps to improve functionality for third-party users.
  • Third-Party API Integration: Easily incorporate external APIs into your scripts.
  • Separating Logic: Isolate utility functions from business logic to enhance maintainability.

Note: Custom modules are not supported in bundle installation scripts.

Preparing to Create a Custom Module

Before starting the creation of a custom module, it’s essential to understand several foundational topics:

  • SuiteScript 2.x Script Basics: Familiarize yourself with the basic structure and laws governing scripts in this version.
  • Entry Point Script Validation: Learn how to validate your entry point scripts to prevent execution issues.
  • Global Objects: Understand available global objects that will assist in your scripting tasks.
  • Access Control: Review how to control access to scripts and modules for better security.

Creating Your Custom Module

To build a custom module, leverage the define Object. This object serves as the core of your module, ensuring that your code adheres to SuiteScript standards. Important points include:

  • Each custom module must return an object when executed. This object typically includes functions that handle specific tasks.
  • Utilize JSDoc tags like @NApiVersion to denote the SuiteScript version, thereby avoiding compatibility issues when scripts reference the module.
  • Use @NModuleScope to control access, allowing you to designate whether other scripts can utilize your module. For guidance on this, see the section on Controlling Access to Scripts and Custom Modules.

Example Structure of a Custom Module Script

Each custom module script generally follows the same structural layout:

javascript
1/**
2 * myModule.js
3 * @NApiVersion 2.x
4 * @NModuleScope Public
5 */
6
7define(['N/record'], function(record) {
8 return {
9 // Module functions
10 };
11});

Utilizing Your Custom Module

To incorporate your custom module into your project, you need to create a separate entry point script that loads it via the require Function. Steps include:

  • Upload your module to the NetSuite File Cabinet.
  • Create script and deployment records for the entry point script that references your module.

For details on paths and naming conventions, see related sections on Naming a Custom Module and Module Dependency Paths.

Key Takeaways

  • Custom modules promote code reusability and maintenance in SuiteScript 2.x.
  • Use JSDoc tags for better control and documentation of your modules.
  • Understand access control to enhance script security.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

How do custom modules in SuiteScript 2.x improve code organization?
Custom modules allow developers to group reusable functions into cohesive files, which enhances code readability and maintenance. They facilitate separating utility and business logic for better modularization.
What are the prerequisites for creating a custom module in SuiteScript 2.x?
Before creating a custom module, it's essential to understand SuiteScript 2.x basics, entry point script validation, use of global objects, and access control for scripts to ensure security.
What limitations exist for custom modules in SuiteScript 2.x regarding bundle installation scripts?
Custom modules are not supported in bundle installation scripts, so they cannot be used in this context.
How can I control access to my custom module in SuiteScript 2.x?
Use the JSDoc tag `@NModuleScope` to control whether other scripts can access your custom module, which is crucial for maintaining security and proper use of your code.
Source: Custom Module Prerequisites Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in General

View all General articles →