When running batch updates especially through Scheduled Scripts, it’s critical to ensure that errors are properly logged without stopping the entire process. A single problematic record should not cause the whole update to fail.
This Admin Tip demonstrates a best-practice approach to admin-level error logging when updating Customer records in bulk, allowing admins to track both successful updates and errors with proper context.
As a NetSuite Administrator, you may need to:
- Perform batch updates on Customer records
- Update a custom field across multiple customers
- Ensure errors are logged clearly for troubleshooting
- Allow the script to continue processing even when one record fails
- This is especially useful during data cleanups, migrations, or operational updates.
Key Best Practices Demonstrated
- Uses a Scheduled Script for batch processing
- Limits results during testing to avoid governance issues
- Logs successful updates for audit tracking
- Captures detailed error logs with customer context
- Continues processing even if one record encounters an error
Sample Script: Batch Customer Update with Error Logging
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
/**
* This script demonstrates best practices for admin-level error logging
* when performing batch updates against Customer records in NetSuite.
* It processes customers with an email, attempts to update a custom field,
* logs updates and errors with context, and continues processing on errors.
*/
define(['N/search', 'N/record', 'N/log'], function(search, record, log) {
function execute(context) {
// Create a saved search to retrieve customer records with emails
var customerSearch = search.create({
type: search.Type.CUSTOMER,
filters: [
['email', search.Operator.ISNOTEMPTY, '']
],
columns: ['internalid', 'entityid', 'email']
});
// Fetch first 10 results for demonstration (prevent large script runs while testing)
var searchResults = customerSearch.run().getRange({ start: 0, end: 10 });
// Loop through each result
for (var i = 0; i < searchResults.length; i++) {
var customerId = searchResults[i].getValue('internalid');
var customerName = searchResults[i].getValue('entityid');
var customerEmail = searchResults[i].getValue('email');
try {
// Try to update a custom field (replace 'custentity5' with your own field ID)
record.submitFields({
type: record.Type.CUSTOMER,
id: customerId,
values: {
custentity5: 'Test Completed'
}
});
// Log successful update for audit trail
log.audit({
title: 'Customer Update Successful',
details: 'Customer: ' + customerName + ' (' + customerEmail + ') updated successfully.'
});
} catch (e) {
// Log error, including customer context for easy troubleshooting
log.error({
title: 'Error Updating Customer',
details: 'Customer: ' + customerName +
' (' + customerEmail +
') [ID: ' + customerId + ']: ' +
e.name + ' - ' + e.message
});
// Continue with next customer (script does not stop on error)
}
}
}
return {
execute: execute
};
});
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 automate your transactions using SuiteScript? Feel free to share them here!