Updating an Address Subrecord Example
The following example shows how to update an employee address. On the employee record, each address is stored in a subrecord that is associated with a line in the Address sublist.
This example uses standard mode, but you could also edit the subrecord using dynamic mode. For general details about using either approach to update a sublist subrecord, see Using SuiteScript 2.x to Edit a Subrecord that Occurs 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) {
// Load the record.
var rec = record.load({
type: record.Type.EMPLOYEE,
id: 1863,
isDynamic: false
});
// Retrieve the subrecord to be modified.
var subrec = rec.getSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress',
line: 0
});
// Change a field on the subrecord.
subrec.setValue({
fieldId: 'addr1',
value: '15 Main Street'
});
// Save the record.
try {
var recId = rec.save();
log.debug({
title: 'Record updated successfully',
details: 'Id: ' + recId
});
} catch (e) {
log.error({
title: e.name,
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});