Creating an Inventory Detail Sublist Subrecord Example
The following example shows how to create a purchase order record that includes an inventory detail subrecord. The script adds one line to the item sublist and creates an inventory detail subrecord on that line.
To use this example, you must meet the following prerequisites:
-
The Advanced Bin / Numbered Inventory Management feature must be enabled at Setup > Company > Enable Features , on the Items & Inventory subtab.
-
The item you add to the sublist should be a lot-numbered inventory item.
-
The receiptinventorynumber value must be unique in your system.
This example uses dynamic mode, but you could also add the subrecord using standard mode. For general details about using either approach to add a sublist subrecord, see Using SuiteScript 2.x to Create a Subrecord in a Sublist Field.
To learn about SuiteScript scripting modes, see SuiteScript 2.x Standard and Dynamic Modes
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define([ 'N/record' ],function(record){
function afterSubmit(context)
// Create the purchase order.
var 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.
var 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: 2
});
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 {
var recId = rec.save();
log.debug({
title: 'Record created successfully',
details: 'Id: ' + recId
});
} catch (e) {
log.error({
title: e.name,
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});