๐น What are Script Parameters?
Script parameters let you make your SuiteScripts configurable without hardcoding values.
Examples:
- API credentials (username, token, secret)
- Default subsidiary or location IDs
- Threshold values (e.g., โminimum order amountโ)
- Boolean switches (on/off feature flags)
Instead of rewriting your script, you add a parameter โ deploy the script โ set the parameter value in the deployment record.
๐น Types of Parameters
When you define a parameter in your script record, you choose a type. Common ones:
- Text โ strings like โsandboxโ or โproductionโ
- Integer โ numbers like โ10โ
- Checkbox โ true/false values
- Select โ picklist linked to a NetSuite record (Customer, Account, Subsidiary, etc.)
- Date/DateTime โ scheduling values
๐น Accessing Script Parameters
SuiteScript gives you APIs in the N/runtime module to get parameters:
runtime.getCurrentScript().getParameter({ name: 'custscript_myparam' })- Always use the internal ID of the parameter (starts with
custscript_).
๐น Example 1 โ Simple Text Parameter
Scenario: Add a parameter called custscript_api_url for your REST API endpoint.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/runtime'], (runtime) => {
const execute = () => {
try {
// 1) Get the current script context
const script = runtime.getCurrentScript();
// 2) Fetch parameter value by its internal ID
const apiUrl = script.getParameter({ name: 'custscript_api_url' });
// 3) Use parameter in logic (here we just log it)
log.debug('Configured API URL', apiUrl);
// In a real integration, you would make HTTP calls to this apiUrl
} catch (e) {
log.error('Error using script parameter', e.message);
}
};
return { execute };
});
๐ก Explanation:
- In deployment, set the
custscript_api_urlto something likehttps://api.test.com. - The script can run against different environments without code changes.
๐น Example 2 โ Integer Parameter for Batch Size
Scenario: Control how many records to process per run.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/runtime'], (runtime) => {
const execute = () => {
try {
const script = runtime.getCurrentScript();
// Get integer parameter
const batchSize = parseInt(script.getParameter({ name: 'custscript_batch_size' })) || 50;
log.debug('Batch Size', batchSize);
// Example: fetch that many results in search
// search.run().getRange({ start: 0, end: batchSize });
} catch (e) {
log.error('Error using batch size parameter', e.message);
}
};
return { execute };
});
๐ก Explanation:
- Instead of hardcoding
50, admin can set batch size in the deployment.
๐น Example 3 โ Checkbox Parameter
Scenario: Toggle between Sandbox Mode and Production Mode.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/runtime'], (runtime) => {
const execute = () => {
try {
const script = runtime.getCurrentScript();
// Checkbox parameters return "T" (true) or "F" (false)
const sandboxMode = script.getParameter({ name: 'custscript_sandbox_mode' }) === 'T';
if (sandboxMode) {
log.debug('Mode', 'Running in SANDBOX mode');
// Do test logic here
} else {
log.debug('Mode', 'Running in PRODUCTION mode');
// Do production logic here
}
} catch (e) {
log.error('Error using sandbox mode parameter', e.message);
}
};
return { execute };
});
๐ก Explanation:
- Helps switch environments without editing the code.
๐น Example 4 โ Select Parameter (Record Reference)
Scenario: A script should always post to a specific Account (from Chart of Accounts).
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/
define(['N/runtime'], (runtime) => {
const beforeSubmit = (context) => {
try {
const script = runtime.getCurrentScript();
// Select parameter stores the internal ID of the chosen record
const defaultAccount = script.getParameter({ name: 'custscript_default_account' });
if (defaultAccount) {
const rec = context.newRecord;
rec.setValue({
fieldId: 'account',
value: defaultAccount
});
log.debug('Account Set', `Default account ${defaultAccount} applied`);
}
} catch (e) {
log.error('Error applying default account', e.message);
}
};
return { beforeSubmit };
});
๐ก Explanation:
- The parameter type is Select โ Account.
- Admin picks an Account in deployment.
- Script automatically applies it.
๐น Best Practices
- Prefix internal IDs with
custscript_for consistency. - Always validate parameter values (check for null/empty).
- Use parseInt/parseFloat for numeric params.
- Use
=== 'T'for checkboxes. - Store credentials (API keys, secrets) in Script Parameters or NetSuiteโs Credential fields, never in code.
- If parameter values differ per subsidiary or deployment, create multiple deployments with different values.
โ Key Takeaway
Script Parameters make your SuiteScripts flexible and reusable. Instead of hardcoding, you configure values at deployment time โ making it easier for admins to manage and developers to maintain.
Leave a Reply