<img src="https://ws.zoominfo.com/pixel/5BFMW73xT6Cu70sN1cUt" width="1" height="1" style="display: none;">

Centralizing Environment Variables in NetSuite SuiteScript with a Shared Library

Posted by Steven Chao on Jul 3, 2025 9:00:00 AM
Steven Chao

Learn how to centralize environment-related variables in NetSuite SuiteScript using a shared module, with examples, benefits, and implementation steps.

Centralizing Environment Variables in NetSuite SuiteScript with a Shared Library

 

Introduction

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.

 

 

Why Centralize Environment Variables?

  • 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.

 

Solution Overview

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.

 

Example: Shared Environment Variable Library

1. Environment Variable Library Script

/**
* @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.

 

2. Using the Shared Library in SuiteScript

You can use this shared library in any SuiteScript file by adding it as a dependency in the define array.

Example: Adding a Button with Environment Variables

/**
* @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.

 

How to Implement

  1. Create the Shared Library:
    Place your environment variable library in a central location (e.g., /SuiteScripts/Shared/Env_Var_Library.js).
  2. Reference in Scripts:
    Add the library as a dependency in your SuiteScript files using the define
  3. Access Variables:
    Use get('VARIABLE_NAME') to retrieve values.
  4. Update as Needed:
    When environment values change, update only the shared library.

 

Summary

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

 

Tags: NetSuite, SuiteScript, Environment Variables, Shared Library, Script Configuration, NetSuite Best Practices

Subscribe to our blog!

Recent Posts

Posts by Topic

see all

Request a quote