Creating an Order Schedule Sublist Subrecord Example
The following example creates a blanket purchase order record. It adds one line to the item sublist and creates an order schedule subrecord on that line.
To use this example, enable the Blanket Purchase Order feature at Setup > Company > Enable Features, on the Transactions subtab.
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 Creating a Sublist Subrecord in SuiteScript 2.1.
To learn about SuiteScript scripting modes, see SuiteScript 2.1 Standard and Dynamic Record Modes
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([ 'N/record' ], (record) => {
const afterSubmit = (context) => {
// Create the record.
const rec = record.create({
type: record.Type.BLANKET_PURCHASE_ORDER,
isDynamic: true
});
// Set body fields on the record.
rec.setValue({
fieldId: 'entity',
value: '1663'
});
rec.setValue({
fieldId: 'location',
value: '6'
});
rec.setValue({
fieldId: 'memo',
value: '456789'
});
// Create one line in the item sublist.
rec.selectNewLine({
sublistId: 'item',
line: 0
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: '500'
});
rec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: '1'
});
// Create the subrecord for that line.
const subrec = rec.getCurrentSublistSubrecord({
sublistId: 'item',
fieldId: 'orderschedule'
});
// Set a field on the body of the subrecord.
subrec.setValue({
fieldId: 'createpurchaseorder',
value: 'LEAD'
});
// Create a line in the subrecord's sublist.
subrec.selectNewLine({
sublistId: 'schedule',
});
subrec.setCurrentSublistValue({
sublistId: 'schedule',
fieldId: 'quantity',
value: 1
});
const nextQuarter = new Date();
nextQuarter.setDate(nextQuarter.getDate() + 90);
subrec.setCurrentSublistValue({
sublistId: 'schedule',
fieldId: 'trandate',
value: nextQuarter
});
// Save the line in the subrecord's sublist.
subrec.commitLine({
sublistId: 'schedule'
});
// 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 };
});