Creating an Order Schedule Sublist Subrecord Example

The following example creates a blanket purchase order record. It creates one line in the item sublist and creates an order schedule subrecord on that line.

To use this example, the Blanket Purchase Order feature must be enabled at Setup > Company > Enable Features, on the Transactions subtab.

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 record.
        var 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.
        var 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
        });

        var 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 {
            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
    };
}); 

        

Related Topics

Using SuiteScript 2.x to Create a Subrecord in a Sublist Field
Creating an Inventory Detail Sublist Subrecord Example
Creating an Address Sublist Subrecord Example
Creating a Landed Cost Sublist Subrecord Example

General Notices