Learn how to centralize environment-related variables in NetSuite SuiteScript using a shared module, with examples, benefits, and implementation steps.
Managing environment-specific variables (such as script IDs, deployment IDs, and folder IDs) is a common challenge in NetSuite SuiteScript development. Hardcoding these values in multiple scripts can lead to maintenance headaches and errors, especially when moving between sandbox, production, or other environments.
A best practice is to centralize these variables in a shared library module. This article explains the benefits, shows how to implement a shared environment variable library, and demonstrates how to use it in your SuiteScript projects.
Maintainability: Update environment-specific values in one place, not scattered across scripts.
Consistency: Prevent mismatches and typos by referencing a single source of truth.
Environment Awareness: Easily support multiple environments (sandbox, production, etc.) with clear separation.
Reusability: Share the same configuration across Client Scripts, User Event Scripts, Suitelets, and more.
The approach is to create a SuiteScript module (library) that exports a function to retrieve environment variables based on the current NetSuite environment. Other scripts can then define this module as a dependency and use its getter function.
/**
* @NApiVersion 2.1
*/
//can reference env type with https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4296647065.html
//use the possible values: SANDBOX, PRODUCTION, BETA, INTERNAL as keys for environment variables
define(['N/runtime'], (runtime) => {
const ENV_VAR = {
"SANDBOX": {
COMPANY_ID:
'YOUR_SANDBOX_COMPANY_ID', SAVE_FILE_SCRIPT_ID: 'YOUR_SANDBOX_SCRIPT_ID',
SAVE_FILE_FOLDER_ID: 'YOUR_SANDBOX_FOLDER_ID',
// ...other variables...
},
"PRODUCTION": {
COMPANY_ID: 'YOUR_PRODUCTION_COMPANY_ID',
SAVE_FILE_SCRIPT_ID: 'YOUR_PRODUCTION_SCRIPT_ID',
SAVE_FILE_FOLDER_ID: 'YOUR_PRODUCTION_FOLDER_ID',
// ...other variables...
}
// Add other environments as needed
};
function get(key) {
const environment = runtime.envType;
return ENV_VAR[environment] && ENV_VAR[environment][key];
}
return { get };
});
Key Points: - Uses the N/runtime module to detect the current environment. - Stores variables in a structured object by environment. - Exposes a get(key) function for easy access.
You can use this shared library in any SuiteScript file by adding it as a dependency in the define array.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/url', './Env_Var_Library'], (url, envVar) => {
const beforeLoad = (scriptContext) => {
if (scriptContext.type === scriptContext.UserEventType.VIEW) {
let suiteletURL = url.resolveScript({
scriptId: envVar.get('SAVE_FILE_SCRIPT_ID'),
deploymentId: envVar.get('SAVE_FILE_DEPLOYMENT_ID'),
returnExternalUrl: false,
params: { recordId: scriptContext.newRecord.id }
});
scriptContext.form.addButton({
id: 'custpage_save_file_button',
label: 'Save File',
functionName: `function
saveFile(){window.open('${suiteletURL}', '_blank');} saveFile`
});
}
};
return { beforeLoad };
});
Benefits: - No hardcoded script or deployment IDs. - Seamless switching between environments.
Centralizing environment variables in a shared SuiteScript library improves maintainability, consistency, and scalability of your NetSuite customizations. By referencing a single source for environment-specific values, you reduce errors and simplify deployments across sandbox, production, and other environments.
Adopt this pattern to streamline your SuiteScript projects and make your codebase easier to manage.
About Us
Concentrus is a leading provider of innovative cloud-based enterprise resource planning (ERP) solutions, including NetSuite. Our team of NetSuite experts offers a range of services, including NetSuite price analysis, NetSuite training, and NetSuite integration services.
Our goal is to help businesses of all sizes maximize their investment in NetSuite by providing expert NetSuite cost optimization and implementation strategies. With years of experience as a NetSuite partner, our NetSuite administrators and NetSuite consultants are well equipped to help businesses of all sizes with their NetSuite consulting needs.
Whether you're looking for a NetSuite consultant to help with your NetSuite implementation or you need ongoing NetSuite support, Concentrus is here to help.
Read About Our Implementation Methodology
Want more NetSuite Tips and Tricks? Check out our Short & 'Suite videos