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 a line to the item sublist and creates an inventory detail subrecord on that line.
To use this example, make sure you meet these requirements:
-
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.
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 or not 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 example in this topic to see how this field is used for the purchase order transaction.
-
This example uses dynamic mode, but you can also use standard mode to add the subrecord. 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
};
});