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.

·3 min read·View Oracle Docs

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:

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:

  1. Define the Module: Use define to specify your module's dependencies and the associated callback function.
  2. 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.
  3. JSDoc Tags: Utilize JSDoc tags like @NApiVersion and @NModuleScope to 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:

javascript
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: true
12 });
13 // Set record properties
14 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: createRecord
24 };
25});

Using Your Custom Module

To utilize your newly created module, create another script to reference it:

  1. Reference Using the Require Function: Load your custom module using an absolute path with the require function.
  2. Deploy the Scripts: Both the custom module and the entry point script must be uploaded to the NetSuite File Cabinet under SuiteScripts.
  3. 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 define method 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.

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

Frequently Asked Questions (4)

Can custom modules be used in bundle installation scripts?
No, custom modules cannot be utilized in bundle installation scripts.
What are the prerequisites to start developing custom modules in SuiteScript 2.x?
Before developing custom modules, you should be familiar with SuiteScript 2.x script basics, entry point script validation, global objects in SuiteScript 2.x, module dependency paths, and controlling access.
How do you define a custom module in SuiteScript 2.x?
To define a custom module, use the 'define' function to specify your module's dependencies and the associated callback function. The module should return an object, typically a function executed in the script's context.
What steps must be taken after creating a custom module to ensure it's used properly?
After creating a custom module, use the 'require' function to load it in other scripts, ensure both the module and entry point script are uploaded to the NetSuite File Cabinet, and create the necessary script and deployment records.
Source: Creating a Custom Module 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 →