As an administrators and developers, we often need to automate complex processes that involve interacting with records—such as retrieving, auditing, and updating Customer information. The introduction of SuiteScript 2.1 offers us powerful modern JavaScript features like async/await and promise-based APIs, making record operations more intuitive and maintainable.
In this article, we will walk through a real-world script example that leverages asynchronous processing to safely load a Customer record, log key details, check for missing information, and make targeted updates. This example demonstrates best practices for field validation, error handling, and logging, providing a template you can adapt for your own administrative automation tasks.
/**
* @NApiVersion 2 .1
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log'], (record, log) => {
/**
* The main entry point for the scheduled script.
* This example demonstrates best practices in record loading, conditional field updating, and error handling.
*/
const execute = async (context) => {
// Replace with a valid Customer Internal ID for your environment.
const CUSTOMER_ID = 123;
try {
// Asynchronously load the Customer record using the promise-based API
const customerRecord = await record.load.promise({
type: record.Type.CUSTOMER,
id: CUSTOMER_ID
});
// Log essential identifying info for admin auditing
log.audit({
title: 'Loaded Customer',
details: `ID: ${CUSTOMER_ID}, Name: ${customerRecord.getValue('entityid')}`
});
// Check and update the phone number if it's currently blank
const currentPhone = customerRecord.getValue('phone');
if (!currentPhone) {
const defaultPhone = '800-555-1234';
customerRecord.setValue({ fieldId: 'phone', value: defaultPhone });
log.debug({
title: 'Phone Updated',
details: `Customer ID ${CUSTOMER_ID} phone set to ${defaultPhone}`
});
// Save the changes asynchronously
const savedRecordId = await customerRecord.save.promise();
log.audit({
title: 'Customer Record Saved',
details: `Updated Customer ID: ${savedRecordId}`
});
} else {
log.debug({
title: 'Phone Already Set',
details: `Customer ID ${CUSTOMER_ID} phone: ${currentPhone}`
});
}
} catch (error) {
// Comprehensive error logging for admin troubleshooting
log.error({
title: 'Error Processing Customer Record',
details: JSON.stringify({
customerId: CUSTOMER_ID,
message: error.message,
stack: error.stack
})
});
}
};
return { 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!