Creating 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 on the customer record.
Note:
For details about working with a transaction's shipping or billing address, see Scripting Transaction Shipping and Billing Addresses.
Before you use or adapt this example:
-
Replace the sample customer ID (
2163) and item ID (100) with valid IDs from your account. -
If you change the address fields, keep
countryfirst. In dynamic mode, changing it later can reset the address form.
For more information, see SuiteScript 2.1 Standard and Dynamic Record Modes and Address Subrecord Characteristics.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([ 'N/record' ], (record) => {
const afterSubmit = (context) => {
// Create the record.
const 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.
const 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 {
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 };
});