SuiteScript extends, customizes, searches, and processes your NetSuite data. SuiteScript enables full-featured application-level scripting capabilities that support sophisticated procedural logic for client and server scripts and robust debugging.

The latest major version, NetSuite SuiteScript 2.0, extends the capabilities provided by the initial SuiteScript version with an API architecture familiar to JavaScript developers. SuiteScript 2.x refers to the latest minor version of SuiteScript. As of this release, the most recent minor version of NetSuite SuiteScript is 2.1.

SuiteScript 2.x's modularity supports encapsulation, provides intuitive code organization, and ensures there are no global variable or method naming conflicts. Automatic dependency management lets you concentrate on logic instead of dependencies and load order. SuiteScript 2.x is designed to support all standard JavaScript. The supplied SuiteScript 2.x APIs give you programmatic access to NetSuite functionality. For generic logic, you can use custom modules to load your preferred third-party JavaScript APIs.

SuiteScript 2.x API enhances the APIs supported by the previous SuiteScript version with APIs for SFTP file transfer, data caching, search pagination, flat file streaming, enhanced encryption, decryption, and hashing. SuiteScript 2.x also provides asynchronous processing through promises, map/reduce scripts that provide a structured framework for server scripts processing many records, and support for all HTTP content types.

Why is it Important for Companies to Use SuiteScript 2.x for development?

There are several advantages to using SuiteScript 2.x. The most significant improvement is the addition of the API modules. Unlike SuiteScript 1.0, modules in SuiteScript 2.x only load the needed data instead of everything, which reduces the load time and computing power required to perform the functions. Another improvement is the change of syntax to a JavaScript format; this allows for a flattened learning curve and the use of third-party APIs such as USPS, FedEx, and PayPal within your own NetSuite system.

SuiteScript 2.x is a complete redesign of the SuiteScript model used in SuiteScript 1.0. Let’s discuss several advantages SuiteScript 2.x has over SuiteScript 1.0.

1. Complexity Management and Intuitive Code Organization

SuiteScript 2.x is built on modularity. Modern SuiteAppsrequire complex scripts that typically consist of many lines of code and many files. Modularity gives users built-in complexity management. It also adds encapsulation, provides intuitive code organization, and ensures that there are no global variable or method naming conflicts.

SuiteScript 2.x comes with a complete set of new APIs contained within modules. These modules are organized and named based on behavior. For example, you use the N/file module when you need to work with files in NetSuite and your script loads only those modules that it needs. With SuiteScript 1.0, all APIs are contained in the same global namespace. Each SuiteScript 1.0 script utilizes the entire namespace, regardless of which APIs it uses.

SuiteScript 2.x also enables you to create your own custom modules. You can use these custom modules to organize your code (as a replacement for SuiteScript 1.0 libraries). Additionally, you can add custom modules to SuiteApps and expose those modules to third parties.

2. Automatic Dependency Management

SuiteScript 2.x gives you built-in dependency management. With SuiteScript 2.x, you define the SuiteScript 2.x modules and custom modules that must load before script execution. The module loader automatically loads those modules’ dependencies, the dependencies, and so forth. Automatic dependency management lets you concentrate on logic instead of dependencies and load order.

3. Modern Programming Syntax and Behavior

The underlying principle of SuiteScript 2.x is that it is like JavaScript. This results in a decreased learning curve for experienced JavaScript developers. The syntax is straightforward JavaScript, and the behavior is consistent. Enhancements to syntax and behavior include the following:

  • Third-Party JavaScript API Support: SuiteScript 2.x is designed to support all standard JavaScript. The supplied SuiteScript 2.x APIs give you programmatic access to NetSuite functionality. For generic logic, use custom modules to load your preferred third-party JavaScript APIs.
  • SuiteScript 1.0 nlapi/nlobj Prefix Retirement: SuiteScript 2.x is modeled to look and behave like modern JavaScript. To meet that objective, SuiteScript 2.x methods and objects are not prefixed with nlapi or nlobj. This change also reflects the modular organization of SuiteScript 2.x. SuiteScript 1.0 methods and objects belong to the nlapi and nlobj namespaces, respectively. SuiteScript 2.x methods and objects are encapsulated within various modules.
  • Usage of Properties and Enumerations: SuiteScript 2.x adopts the usage of properties and enumerations. Most SuiteScript 1.0 getter and setter methods are replaced with properties in SuiteScript 2.x and enumerations encapsulate common constants (for example, standard record types).
  • Updated Sublist and Column Indexing: The standard practice in the developing world is to start indexing at 0 and this behavior is observed in most programming languages, while SuiteScript 1.0 starts sublist and column indexing at 1. To align SuiteScript with modern JavaScript, sublist and column indexing within SuiteScript 2.x begins at 0.
    A recent version of SuiteScript, NetSuite SuiteScript 2.1, is also available. This version is the latest minor version of SuiteScript. It extends SuiteScript 2.x by supporting additional ECMAScript language features and syntax.

4. Functionality Enhancements

The following enhancements are exclusive to SuiteScript 2.x:

  • Map/Reduce Script Type: SuiteScript 2.x introduces a new server script type based on the map/reduce model. Map/reduce scripts provide a structured framework for server scripts that process many records. In addition, Suite Cloud Plus users can also use map/reduce scripts to process records in parallel across multiple processors. Users manually select the number of processors from the script deployment record.
  • Asynchronous Processing (Promises): Promises are JavaScript objects that represent the eventual result of an asynchronous process. After these objects are created, they serve as placeholders for an operation’s future success or failure. The remaining segments of the script can execute during the period that a Promise object is waiting in the background.
    • In SuiteScript 2.x, all client scripts, and a subset of server scripts, support the use of promises. With promises, developers can write asynchronous code that is intuitive and efficient. SuiteScript 2.x provides promise APIs for selected modules. In addition, you can create custom promises in all client scripts.
  • Secure File Transfer Protocol (SFTP) File Transfer API: SuiteScript 2.x provides support for SFTP. This feature lets you securely transfer files between NetSuite and external File Transfer Protocol (FTP) servers. SFTP is a protocol packaged with Secure Shell (SSH). It is like FTP, but files are transferred over a secure connection. Server authorization requires a password, Globally Unique Identifier (GUID), and a Digital Signature Algorithm (DSA), Elliptical Curve Digital Signature Algorithm (ECDSA), or a cryptosystem (RSA) host key.
  • Cache API: The SuiteScript 2.x Cache API enables you to load data into a cache and make it available to one or more scripts. This feature reduces the amount of time required to retrieve data.
  • Search Pagination API: The SuiteScript 2.x Search Pagination API enables you to page through search results. This enhancement increases script performance and gives you an intuitive means to efficiently traverse search result data.
  • Flat File Streaming API: With SuiteScript 1.0, you cannot easily access the contents of files that are over 10MB. You must partition your files into smaller, separate files to read, write, and append file contents in memory.
    The SuiteScript 2.x Flat File Streaming API enables you to process and stream large CSV and plain text files efficiently. With this enhancement, you can load and edit each line of content into memory and then append the lines back together. The Flat File Streaming API enforces the 10MB limit only on individual lines of content.
  • Expanded Support for HTTP Content-Type Headers: SuiteScript 2.x adds support for all HTTP content types. This enhancement applies to both client requests and server response HTTP headers.

With the help of the above advantages and functionalities in suite script 2.x, companies can save development time, cut costs, and make the system most efficient for day-to-day business operations.

How Does SuiteScript 2.x Work?

To help you understand how SuiteScript 2.x works, let’s walk through the SuiteScript 2.0 example of basic customization. After completing the steps below, the system displays a “Hello, World!” message whenever you load a NetSuite task record.

Step 1: Enable the Feature

To enable the Client SuiteScript feature:

  1. Select Setup > Company > Enable Features.
  2. Click the Suite Cloud subtab.
  3. Locate the Client SuiteScript option. If the box is already checked, skip ahead to Step Create the Script File. If it is not, check the box.
  4. If you agree to the terms, scroll to the bottom of the window, and click I Agree.
  5. Server SuiteScript is not required for the steps described in this topic; however, if you plan to do further SuiteScript development, consider checking the Server SuiteScript box. If you check the box, the system displays another window listing the terms of service. Click I Agree.
  6. Click Save.

Step 2: Create the Script File

To create the script file:

  1. Open a new file in your text editor of choice.
  2. Add two JSDoc tags @NApiVersion and @NScriptType. After each JSDoc tag, add the appropriate value as shown in the following snippet.

/**

*@NApiVersion 2.x

*@NScriptType ClientScript

*/

Add the define Object. Every entry point script must use this function.

Use [‘N/ui/dialog’] as the define function in the first argument. This first argument is an array of string values representing the modules that the function should load.

define(['N/ui/dialog'],

// In Step 4, you will add additional code here.

);

Declare a callback function. The callback function is the define function’s second argument. Give this function one argument called dialog.

function(dialog) {

// In Step 5, you will add additional code here.

}

Within the callback function, declare a function called helloWorld.

function helloWorld(){

// In steps 6-10, you will add additional code here.

}

Within the entry point function, create an object named options.

var options = {

title: 'Hello!',

message: 'Hello, World!'

};

Add a try/catch statement.

This statement is not required; however, this approach lets your script handle errors gracefully. That is, if an error occurs and is handled by a try/catch statement, your script, and any others that are deployed on the page, can continue executing. Using a try/catch statement can also help you understand why problems occur. Note that the try/catch keywords are part of JavaScript and not specific to SuiteScript 2.x.

try {

// In steps 8 and 9, you will add additional code here.

} catch (e) {

// In Step 10, you will add an extra code here.

}

For the first action of the try block

invoke the N/ui/dialog Module’s alert () method. This method creates a dialog with a title, a message, and an OK button.

dialog.alert(options);

Immediately after the entry point function, add a return statement. In every SuiteScript 2.x entry point script, the return statement must include at least one line that has two components:

  • An entry point, which in this case is pageInit(scriptContext).
  • A function, which in this case is helloWorld.

return {

pageInit: helloWorld

};

Save the file, naming it helloWorld.js.

Step 3: Upload the Script File to NetSuite

To upload the script file:

  1. In the NetSuite UI, go to Documents > File > SuiteScripts.
  2. In the left pane, select the SuiteScripts folder and click Add File.
  3. Follow the prompts to locate the helloWorld.js file in your local environment and upload it.

Step 4: Create a Script Record and Script Deployment Record

Before an entry point script can be executed in your account, you must create a script record that represents the entry point script file. You must also create a script deployment record.

The script deployment record contains part of the logic that determines when the script executes. Some of that logic is contained within the script, by the entry point that the script uses. For example, by using the pageInit(scriptContext) entry point, you tell the system that the script should execute when a page loads. However, you must also identify the specific page that, when loaded, causes the script to execute. Stated another way, you must tell the system which record type this script should execute on. To do that, you use the script deployment record.

Step 5: Test the Script

To test the script, verify that the dialog alert appears when it should:

  • Open a task record by going to Activities > Scheduling > Tasks > New.
  • If the script is working properly, the system displays a dialog alert.

Conclusion

SuiteScript 2.x brings some long-awaited and exciting JavaScript features. We started writing SuiteScript 2.x, which gives us a rich set of features and good performance of the system.

Jade as an Oracle NetSuite ERP Implementation Partner

Jade is a strongly tied NetSuite Systems Integrator partner delivering functional and technical implementations, optimization services, and managed services. We combine our in-depth NetSuite knowledge with our broad domain expertise to provide the best value for our customers during implementation and beyond.

If you are interested in our NetSuite ERP Services and want a demo of how Suite Script 2.x can help your business, please visit our webpage on NetSuite Implementation Services or connect with our experts

Subscribe to our email Newsletter

Popular Posts

Tags

About the Author

Avej Shaikh

Avej Shaikh

Senior Technical Analyst

Avej has been working as a Senior Technical Analyst at Jade since March 2022 and holds a bachelor’s degree in Computer Science and Engineering. He has more than 5 years of ERP domain experience and has worked on  multiple NetSuite modules like Order Management, Procurement, FAM, Inventory Management, and Manufacturing, etc. He is also experienced in Customizations, Integrations, Suite Flow projects, and SuiteApps such as Warehouse Management Systems and Delivery Routing. 

How Can We Help You?