Using SuiteScript 2.x to Create a New Shipping Address Example

The following example creates a sales order record with one line in the item sublist. It also creates a shipping address for the transaction. It overrides any default shipping address that might be defined on the customer record.

Note:

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

To use this example, you must meet the following prerequisites:

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

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

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

        rec.setValue({
            fieldId: 'memo',
            value: '102A'
        });

        // Create the subrecord.
        var subrec = rec.getSubrecord({
            fieldId: 'shippingaddress'
        });

        // Set values on the subrecord.
        // Set country field first when script uses dynamic mode
        subrec.setValue({
            fieldId: 'country',
            value: 'US'
        });

        subrec.setValue({
            fieldId: 'city',
            value: 'New York'
        });

        subrec.setValue({
            fieldId: 'state',
            value: 'New York'
        });

        subrec.setValue({
            fieldId: 'zip',
            value: '10018'
        });

        subrec.setValue({
            fieldId: 'addr1',
            value: '8 W 40th St.'
        });

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

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

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

        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 Select an Existing 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