When working with transaction records such as Sales Orders, understanding how your script flows behind the scenes is essential for effective troubleshooting and validation. One of the simplest yet most powerful tools available to NetSuite admins and developers is the use of log.debug.
By placing log.debug statements at key points in your script, you gain visibility into record values, script progress, and potential failures—making it significantly easier to identify issues early.
Example Use Case: Updating a Sales Order with Execution Tracing
Below is a simple example of how you can use log.debug to trace the full lifecycle of a Sales Order update within a Scheduled Script:
/**
* @NApiVersion 2 .x
* @NScriptType ScheduledScript
*/
// Update Sales Order Status Example with log.debug tracing
define(['N/log', 'N/record', 'N/search'], function(log, record, search) {
function execute(context) {
// Log the start of the script execution
log.debug({
title: 'Script Start',
details: 'Beginning Sales Order update process.'
});
var salesOrderId = 12345; // Sample Sales Order internal ID
try {
// Log before loading the Sales Order
log.debug({
title: 'Loading Sales Order',
details: 'Loading Sales Order ID: ' + salesOrderId
});
// Load the Sales Order record
var salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: salesOrderId
});
// Log after loading, showing relevant initial values
log.debug({
title: 'Sales Order Loaded',
// details will be stringified if it's an object
details: { status: salesOrder.getValue('status'), entity: salesOrder.getValue('entity') }
});
// Update the memo field on the Sales Order
salesOrder.setValue({
fieldId: 'memo',
value: 'Updated by script on ' + new Date().toISOString()
});
// Log after making a change, before saving the record
log.debug({
title: 'Memo Updated',
details: 'Memo field changed. Saving record...'
});
var salesOrderIdSaved = salesOrder.save(); // Save the Sales Order and log the resulting ID
log.debug({
title: 'Sales Order Saved',
details: 'Sales Order successfully saved. ID: ' + salesOrderIdSaved
});
} catch (e) {
// Log any errors that occur during execution
log.debug({
title: 'Error Occurred',
details: e.message // Log only the error message, not sensitive details
});
}
// Log the end of the script execution
log.debug({
title: 'Script End',
details: 'Sales Order update script completed.'
});
}
return { execute: execute };
});
- log.debug is your best friend when validating script behavior.
- Logging initial and updated values helps you confirm that your script is working as intended.
- Adding clear log titles and details improves readability in Execution Logs.
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!