Creating an Inventory Detail Sublist Subrecord Example
The following examples create a purchase order with one lot-numbered inventory item. Each example creates an inventory detail subrecord on the item line to store a lot number:
Before using these examples in your NetSuite account, do the following:
-
Enable Advanced Bin / Numbered Inventory Management at Setup > Company > Enable Features, on the Items & Inventory subtab.
-
Verify that your account has a vendor, a location, and a lot-numbered inventory item. Note their internal IDs.
-
Replace the hardcoded IDs where indicated in the script comments.
-
Use a unique value for
receiptinventorynumber.
Note how this field differs from the issueinventorynumber field, both available within the inventory detail subrecord, in terms of usage. Make sure that your script does not set both fields at the same time. Use the appropriate field based on whether the transaction adds items to your inventory, as follows:
-
Issue Inventory Number (ID: issueinventorynumber) - In this field, you can select lot or serial numbers that exist in your inventory. Be sure to reference this field to select numbers for items that you include in a sales order, item fulfillment, inventory status change, and negative adjustments, among others.
-
Receipt Inventory Number (ID: receiptinventorynumber) - In this field, you can enter lot or serial numbers for items that you want to add to your inventory. Be sure to use this field to assign numbers to items within item receipts and positive adjustments, among others. Refer to the examples in this topic to see how this field is used for the purchase order transaction.
For general details about creating a sublist subrecord in either mode, see Creating a Sublist Subrecord in SuiteScript 2.1.
For information about record modes, see SuiteScript 2.1 Standard and Dynamic Record Modes.
Inventory Detail Subrecord Example in Standard Mode
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record'], (record) => {
const myAfterSubmit = (context) => {
// Create the purchase order.
const rec = record.create({
type: record.Type.PURCHASE_ORDER,
isDynamic: false
});
// Set body fields on the purchase order. Replace both of these
// hardcoded values with valid values from your NetSuite account.
rec.setValue({
fieldId: 'entity',
value: '2'
});
rec.setValue({
fieldId: 'location',
value: '2'
});
// Insert a line in the item sublist.
rec.insertLine({
sublistId: 'item',
line: 0
});
// Set the required fields on the line. Replace the hardcoded value
// for the item field with a valid value from your NetSuite account.
rec.setSublistValue({
sublistId: 'item',
fieldId: 'item',
line: 0,
value: '7'
});
rec.setSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: 0,
value: 1
});
// Instantiate the subrecord. To use this method, you must
// provide the ID of the sublist, the number of the line you want
// to interact with, and the ID of the summary field.
const subrec = rec.getSublistSubrecord({
sublistId: 'item',
line: 0,
fieldId: 'inventorydetail'
});
// Insert a line in the subrecord's inventory assignment sublist.
subrec.insertLine({
sublistId: 'inventoryassignment',
line: 0
});
subrec.setSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
line: 0,
value: 1
});
// Set the lot number for the item. Replace this example value
// with the lot number you want to assign.
subrec.setSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'receiptinventorynumber',
line: 0,
value: '01234'
});
// Save the record. Note that the subrecord object does
// not have to be explicitly saved.
try {
const recId = rec.save();
log.debug({
title: 'Record created successfully',
details: 'Id: ' + recId
});
} catch (e) {
log.error({
title: e.name,
details: e.message
});
}
};
return {
afterSubmit: myAfterSubmit
};
});
Inventory Detail Subrecord Example in Dynamic Mode
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([ 'N/record' ], (record) => {
const afterSubmit = (context) => {
// Create the purchase order.
const rec = record.create({
type: record.Type.PURCHASE_ORDER,
isDynamic: true
});
// Set body fields on the purchase order.
rec.setValue({
fieldId: 'entity',
value: '1663'
});
rec.setValue({
fieldId: 'location',
value: '6'
});
// Create one line in the item sublist.
rec.selectNewLine({
sublistId: 'item'
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: '299'
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1
});
// Create the subrecord for that line.
const subrec = rec.getCurrentSublistSubrecord({
sublistId: 'item',
fieldId: 'inventorydetail'
});
// Add a line to the subrecord's inventory assignment sublist.
subrec.selectNewLine({
sublistId: 'inventoryassignment'
});
subrec.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
value: 1
});
subrec.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'receiptinventorynumber',
value: '01234'
});
// Save the line in the subrecord's sublist.
subrec.commitLine({
sublistId: 'inventoryassignment'
});
// Save the line in the record's sublist
rec.commitLine({
sublistId: 'item'
});
// Save the record.
try {
const recId = rec.save();
log.debug({
title: 'Record created successfully',
details: 'Id: ' + recId
});
} catch (e) {
log.error({
title: e.name,
details: e.message
});
}
};
return { afterSubmit };
});
For additional script samples, see Scripting Sublist Subrecords and Scripting Body Field Subrecords.