Updating an Address Sublist Subrecord Example

The following example shows how to update an employee address. Each address on the employee record is stored in a subrecord linked to a line in the Address sublist.

This example uses standard mode, but you can also use dynamic mode to edit the subrecord. For general details about using either approach to update a sublist subrecord, see Updating 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) => {
        // Load the record.
        const rec = record.load({
            type: record.Type.EMPLOYEE,
            id: 1863,
            isDynamic: false
        });

        // Retrieve the subrecord to be modified.
        const 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 {
            const recId = rec.save();
            log.debug({
                title: 'Record updated successfully',
                details: 'Id: ' + recId
            });
        } catch (e) {
             
            log.error({
                title: e.name,
                details: e.message
            });
        }
    };
    return { afterSubmit };
}); 

        

Related Topics

General Notices