Creating an Address Sublist Subrecord Example
The following example shows how to create an employee record and populate the Address sublist with one line. The script also creates an address subrecord on the sublist line.
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.EMPLOYEE,
isDynamic: true
});
// Set the required body fields.
rec.setValue({
fieldId: 'firstname',
value: 'John'
});
rec.setValue({
fieldId: 'lastname',
value: 'Smith'
});
rec.setValue({
fieldId: 'subsidiary',
value: '1'
});
// Create a line in the Address sublist.
rec.selectNewLine({
sublistId: 'addressbook'
});
// Set an optional field on the sublist line.
rec.setCurrentSublistValue({
sublistId: 'addressbook',
fieldId: 'label',
value: 'Primary Address'
});
// Create an address subrecord for the line.
const subrec = rec.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
// Set body fields on the subrecord. Because the script uses
// dynamic mode, you should set the country value first. The country
// value determines which address form is to be used, so by setting
// this value first, you ensure that the values for the rest
// of the form's fields will be set properly.
subrec.setValue({
fieldId: 'country',
value: 'US'
});
subrec.setValue({
fieldId: 'city',
value: 'San Mateo'
});
subrec.setValue({
fieldId: 'state',
value: 'CA'
});
subrec.setValue({
fieldId: 'zip',
value: '94403'
});
subrec.setValue({
fieldId: 'addr1',
value: '2955 Campus Drive'
});
subrec.setValue({
fieldId: 'addr2',
value: 'Suite 100'
});
// Save the sublist line.
rec.commitLine({
sublistId: 'addressbook'
});
// 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 };
});