ANS-0767 · SUITESCRIPT DEVELOPMENT
How to Declare and Manage Constants in NetSuite SuiteScript?
Effectively declare constants in SuiteScript to avoid conflicts with browser variables and other deployed scripts.
Short answer
To prevent conflicts with browser objects and other scripts, encapsulate constants within a unique object. While manual encapsulation is effective, modern SuiteScript 2.x and 2.1 leverage modular architecture (AMD) for built-in encapsulation and dependency management, offering a more structured approach. Always use 'const' for true constants.
Scenario
When developing SuiteScript, particularly client-side scripts, developers may encounter issues with global variable conflicts. These conflicts can arise from constants sharing names with browser Window object variables or with constants declared in other scripts deployed on the same record, leading to unexpected behavior and data discrepancies.
Solution
To mitigate potential conflicts with browser variables and other scripts, constants should be encapsulated within an object. This object requires a unique identifier, such as the script name, to ensure its uniqueness and prevent naming collisions. While this manual object encapsulation is an effective method for preventing global variable conflicts, it is important to note that modern SuiteScript 2.x and 2.1 development leverages a modular architecture (AMD) that provides built-in encapsulation and dependency management, offering a more structured and idiomatic way to manage code and prevent global namespace pollution. Additionally, modern JavaScript best practices, supported by SuiteScript 2.1, recommend using const for declaring true constants instead of var.
Consider the following examples demonstrating the issues and the encapsulated solution:
Problematic #1: Conflicts with browser variables
To access the browser client information:
javascript
window.clientInformationThis returns an object such as:
Navigator {geolocation: Geolocation, doNotTrack: null, webkitPersistentStorage: StorageQuota, webkitTemporaryStorage: StorageQuota, onLine: true…}However, if a global constant with the same name is declared for another purpose:
javascript
const clientInformation = 989;And an attempt is made to access the browser client information again, this will return:
989This demonstrates that any Window object variable can be overridden if a constant shares the same name.
Problematic #2: Scripts sharing the same constant names
When multiple scripts are deployed on the same record, they may share their global constants. Consider the following examples with two client-side scripts, both triggered on page init, where a constant shares the same name:
Script #1
javascript
const GOOSE_EGG = 0;
function pageInit_test(type) {
alert(GOOSE_EGG);
}Script #2
javascript
const GOOSE_EGG = 1;
function pageInit_test2(type) {
alert(GOOSE_EGG);
}When creating or editing the record where these scripts are deployed, two alert pop-ups will appear, both showing the value:
1Both scripts will use the value from Script #2 (the latest script created in NetSuite).
Solution: Encapsulate constants into an object
To ensure constant names are unique and do not conflict with browser variables or other scripts, encapsulate them into an object. The script name can be used as a unique identifier for this object.
Example:
javascript
const ITR_SUE_AUTOPOPULATE = {};
ITR_SUE_AUTOPOPULATE.MAX_RESULTS = 1000;It can then be referenced as follows:
javascript
ITR_SUE_AUTOPOPULATE.MAX_RESULTSThis approach helps prevent discrepancies and ensures that constant values are localized to their respective scripts.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
