When working with transaction records, it is common to inspect fields on the Item sublist of a Sales Order. One area that can cause confusion is the difference between column metadata and the actual value stored on a transaction line.
For example, the item column on the Sales Order item sublist has a label such as Item. However, that label is not the item selected on the transaction line. It is only information about the column itself.
This article shows how to use SuiteScript to read metadata about a sublist column and then separately retrieve the actual item value from a Sales Order line.
SuiteScript Example
/**
* @NApiVersion 2 .1
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/log'], (record, log) => {
/**
* Main entry point for the Scheduled Script.
*
* This example loads a Sales Order and reads metadata
* about the "item" column on the "item" sublist.
*/
const execute = (context) => {
try {
// Replace this with an existing Sales Order internal ID in your account.
const SALES_ORDER_ID = 12345;
// Internal ID of the sublist on a Sales Order.
// For transaction item lines, the sublist ID is usually "item".
const SUBLIST_ID = 'item';
// Internal ID of the column/field on the item sublist.
// This is the "Item" column.
const COLUMN_FIELD_ID = 'item';
// Load the Sales Order record.
// record.load() loads an existing record.
const salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: SALES_ORDER_ID,
isDynamic: false
});
// Get the item sublist object from the Sales Order.
const itemSublist = salesOrder.getSublist({
sublistId: SUBLIST_ID
});
// Get the column object for the "item" field on the item sublist.
const itemColumn = itemSublist.getColumn({
fieldId: COLUMN_FIELD_ID
});
// Always check that the column exists before using it.
if (!itemColumn) {
log.audit({
title: 'Column Not Found',
details: `Column ${COLUMN_FIELD_ID} was not found on sublist ${SUBLIST_ID}.`
});
return;
}
// Column.label returns information about the column label.
// Example result may be "Item", depending on form/customization/language.
log.audit({
title: 'Column Metadata',
details: {
columnId: itemColumn.id, // Internal column ID, e.g. "item"
columnLabel: itemColumn.label, // UI label, e.g. "Item"
columnType: itemColumn.type, // Column type
sublistId: itemColumn.sublistId, // Sublist that owns the column
isDisplayed: itemColumn.isDisplay, // Whether column is displayed
isMandatory: itemColumn.isMandatory // Whether column is mandatory
}
});
// Important:
// Column.label is NOT the item value on a line.
// It is only metadata about the column.
//
// To get the actual value from a line, use getSublistValue().
const lineCount = salesOrder.getLineCount({
sublistId: SUBLIST_ID
});
if (lineCount > 0) {
// Gets the internal value of the item on line 0.
const firstLineItemValue = salesOrder.getSublistValue({
sublistId: SUBLIST_ID,
fieldId: COLUMN_FIELD_ID,
line: 0
});
// Gets the display text of the item on line 0.
const firstLineItemText = salesOrder.getSublistText({
sublistId: SUBLIST_ID,
fieldId: COLUMN_FIELD_ID,
line: 0
});
log.audit({
title: 'First Line Item Value',
details: {
line: 0,
itemInternalId: firstLineItemValue,
itemDisplayText: firstLineItemText
}
});
} else {
log.audit({
title: 'No Item Lines',
details: 'The Sales Order has no lines on the item sublist.'
});
}
} catch (e) {
// Log any unexpected error.
log.error({
title: 'Error Reading Column Label',
details: `${e.name}: ${e.message}`
});
}
};
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!