Organizations often develop reusable SuiteScript libraries to support multiple transaction types and record customizations. When processing sublist data, it's important to verify that a retrieved column belongs to the expected sublist before performing line-level operations. Using Column.sublistId helps make scripts more reliable and easier to maintain.
Script Sample
This Scheduled Script loads a Sales Order, verifies that the Quantity column belongs to the Items sublist by using Column.sublistId, and then processes each item line only after the validation succeeds.
/**
* @NApiVersion 2 .1
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/log'], (record, log) => {
const execute = () => {
// Load a Sales Order
const salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: 12345 // Replace with a valid Sales Order Internal ID
});
// Get the Items sublist
const itemSublist = salesOrder.getSublist({
sublistId: 'item'
});
// Get the Quantity column from the Items sublist
const quantityColumn = itemSublist.getColumn({
fieldId: 'quantity'
});
// Verify which sublist contains the column
if (quantityColumn.sublistId === 'item') {
log.debug({
title: 'Validation Passed',
details: 'The Quantity column belongs to the Items sublist.'
});
// Process every item line
const lineCount = salesOrder.getLineCount({
sublistId: quantityColumn.sublistId
});
// Loop through each line in the Items sublist
for (let i = 0; i < lineCount; i++) {
// Retrieve the Quantity value from the current line
const quantity = salesOrder.getSublistValue({
sublistId: quantityColumn.sublistId,
fieldId: 'quantity',
line: i
});
// Display the quantity for the current line
log.debug({
title: 'Line '+ (i + 1),
details: 'Quantity: '+ quantity
});
}
} else {
// Display an error if the column does not belong to the Items sublist
log.error({
title: 'Validation Failed',
details: 'The column does not belong to the Items sublist.'
});
}
};
return { execute };
});
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.
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!