Sample of custom module constant file we can use any environment (sandbox and Production). It will only need to find out the constant in the environment and set it in the constant files.
/**
* @NApiVersion 2.1
*/
define([
"N/runtime"
], (runtime) => {
// https://suiteanswers.custhelp.com/app/answers/detail/a_id/61225/loc/en_US
// https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4296530806.html
// these objects will contain the environment specific values we need
const SB2 = {
SHIP_METHOD: {
"UPS Ground": 11
}
}
const SB3 = {
SHIP_METHOD: {
"UPS Ground": 4
}
}
// During feature development, when adding a new constant, add it to all environment objects.
//
// If the constant does not yet exist in the environment, set its value to null.
//
// In this way, we will maintain a set of constants for all environments, and we enable
// error handling in our code.
const PRODUCTION = {
SHIP_METHOD: {
"UPS Ground": null
}
}
// This is our dispatch object that references our above env specific objects.
//
// It uses the account ID values as prop names.
// At runtime when this lib file is loaded in a script, the code will dynamically return the correct
// environment object based on the account ID.
function globalData() {
const DEPLOYMENT_ENVIRONMENT = runtime.accountId;
var CONSTANTS = {
"386782_SB2": SB3,
"386782_SB2": SB2,
"37596_PRODUCTION": PRODUCTION
}
if (DEPLOYMENT_ENVIRONMENT in CONSTANTS) {
return CONSTANTS[DEPLOYMENT_ENVIRONMENT]
} else {
throw new Error("Constants not found for account: ", DEPLOYMENT_ENVIRONMENT)
}
}
return {
globalData: globalData
}
// Throughout our scripts, all we have to do going forward is import this file and reference it like so:
// constants.SHIP_METHOD["UPS Ground"]
// Or, better:
// constants.SHIP_METHOD.UPS_GROUND
// We prefer the second method where possible because the IDE will warn us if we try to access an non-existant property
// constans.SHIP_METHOD.UPS_GROND >> IDE will highlight with a warning!
});