When building SuiteScript solutions, it is important to anticipate potential errors during runtime—especially with operations that rely on external data, integrations, or user input. Wrapping these risky operations inside try/catch blocks lets you gracefully capture and log errors, preventing abrupt script failures and improving system reliability.
How Does Try/Catch Help?
- Captures exceptions: Prevents the script from failing silently or stopping execution unexpectedly.
- Improves supportability: Caught errors can be logged with useful context, helping admins and developers troubleshoot faster.
- Protects user experience: Allows partial completion of tasks, fallback actions, or cleaner error messaging for end users.
Sample Pattern in a Scheduled Script
define(['N/log'], function(log) {
function execute(context) {
try {
// Potentially error-prone logic
var result = riskyOperation();
log.audit({title: 'Operation Succeeded', details: result});
} catch (e) {
log.error({title: 'Error Occurred', details: e.message});
// Optionally, add more details or rethrow if needed
}
}
return { execute: execute };
});
In this example, the try block contains code that might throw an error—such as a function that processes data, performs calculations, or retrieves records.
If an error occurs, control immediately moves to the catch block, where the script logs the error details instead of stopping execution abruptly.
This ensures that even if one operation fails, the rest of the script (or the system) can continue running smoothly.
Sample Scheduled Script with Try/Catch Error Handling
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
define(['N/log', 'N/search'], function(log, search) {
function execute(context) {
try {
// Example: Load a saved search and process results
var mySearch = search.load({ id: 'customsearch_customer_list' });
var searchResultCount = mySearch.runPaged().count;
log.audit({
title: 'Search Succeeded',
details: 'Total records found: ' + searchResultCount
});
// Further processing can go here...
} catch (e) {
log.error({
title: 'Error Occurred',
details: e.message || e.toString()
});
// Optionally, you might rethrow or handle specific errors
}
}
return {
execute: execute
};
});
- The script uses a try/catch block around search and processing logic.
- log.audit reports success with a clear message.
- log.error captures and logs any exception, assisting support and diagnostics.
- This skeleton can be adapted to include any logic that may require error handling.
Incorporate try/catch in your scripts whenever calling APIs, processing external data, or performing any operation susceptible to failure. This proactive error handling makes your scripts more robust and easier to support!
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.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to use try/catch in SuiteScript? Feel free to share them here!