Using SuiteScript 2.x to Select an Existing Shipping Address Example

The following script creates a sales order record with one in line in the item sublist. It also sets the value of the shipping address field to the value of an address from the customer record. Because the script selects an existing address subrecord, it does not use subrecord methods. It identifies the address subrecord by internal ID.

Note:

For details about working with a transaction’s shipping or billing address, see Scripting Transaction Shipping and Billing Addresses.

          /**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */

define([ 'N/record' ],function(record) {
    function afterSubmit(context) {
        var rec = record.create({
            type: record.Type.SALES_ORDER,
           isDynamic: true
       });

        // Set body fields on the record.
        rec.setValue({
            fieldId: 'entity',
            value: '110'
        });

        // Set the shipping address to the value of
        // an address on the customer record.
        rec.setValue({
            fieldId: 'shipaddresslist',
            value: '395713'
        });

        // Create one line in the item sublist.
        rec.selectNewLine({
            sublistId: 'item'
        });

        rec.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'item',
            value: '100'
        });

        rec.setCurrentSublistValue({
            sublistId: 'item',
            fieldId: 'quantity',
            value: '3'
        });

        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

Scripting Transaction Shipping and Billing Addresses
Using SuiteScript 2.x to Create a New Shipping Address Example
Using SuiteScript 2.x to Retrieve a Shipping Address Example
Using SuiteScript 2.x to Retrieve one Value from a Shipping Address Example

General Notices