Scripts can run from different areas of NetSuite, such as the user interface, CSV imports, web services, scheduled processes, or other scripts. If a script should only run in certain situations, checking the execution context can help prevent unintended behavior.
Using execution context helps admins and developers control when script logic should continue and when it should be skipped.
Scenario
A script works correctly when a user edits a record in the NetSuite UI, but it should not run when records are updated by other processes.
Examples may include:
- CSV imports
- web services integrations
- scheduled scripts
- map/reduce scripts
- user event scripts triggered by another process
- mass updates
If the script runs in all contexts, it may cause duplicate updates, unexpected errors, or slower processing.
Solution
Use execution context to check how the script was triggered.
If the script should only run for a specific context, add logic that confirms the context before continuing. If the context does not match, the script can stop processing.
Common Execution Context Examples
Common execution contexts may include:
- User Interface – A user updates the record directly in NetSuite.
- CSV Import – A record is created or updated through CSV import.
- Web Services – An external system creates or updates the record.
- Scheduled Script – A scheduled script triggers processing.
- Map/Reduce Script – A map/reduce script triggers processing.
- Mass Update – A mass update changes records in bulk.
Sample Script
The sample below shows a User Event Script that only continues when the record is updated from the NetSuite user interface.
/**
* @NApiVersion 2 .1
* @NScriptType UserEventScript
*/
define(['N/runtime', 'N/log'], (runtime, log) => {
function beforeSubmit(context) {
const executionContext = runtime.executionContext;
// Skip the script if it was not triggered from the NetSuite user interface.
if (executionContext !== runtime.ContextType.USER_INTERFACE) {
log.audit({
title: 'Script Skipped',
details: `Execution Context: ${executionContext}`
});
return;
}
// Add UI-only logic here.
log.audit({
title: 'Script Continued',
details: 'Script is running from the user interface.'
});
}
return {
beforeSubmit
};
});
How It Works
The script uses:
runtime.executionContext
to identify how the script was triggered.
It then compares the current context to:
runtime.ContextType.USER_INTERFACE
If the script was triggered from the user interface, the logic continues. If it was triggered from another context, such as CSV import or web services, the script logs a message and stops.
When This Helps
Execution context checks are useful when:
- validation should only apply to UI users
- integrations should not trigger certain logic
- CSV imports should bypass user-facing messages
- scheduled processes should avoid duplicate updates
- scripts should behave differently depending on how they are triggered
References
- SA: SuiteScript Client Script Sample (ID: 108198)
- SA: SuiteScript 2.x Script Types (ID: 43520)
- SA: SuiteScript Functions (ID: 10284)
Notes
- Use execution context checks when script behavior should differ by trigger source.
- Do not rely only on deployment settings when logic must be tightly controlled.
- Add clear log messages when the script skips processing.
- Test the script from each expected context, such as UI, CSV import, or integration.
- Avoid skipping important validation unless the business process allows it.
- Document which contexts are allowed and why.
Summary
Execution context checks help control when a script should run. By confirming whether a script was triggered from the user interface, CSV import, web services, or another process, admins and developers can reduce unintended script behavior.
This makes scripts safer, easier to troubleshoot, and easier to maintain across different NetSuite processes.
Disclaimer :The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
Share your insights and experiences in the NetSuite Admin Corner.