Retrieving an Address Subrecord Example

This example loads an employee record and selects a line on the address sublist. It retrieves a value from the sublist line that's not part of the subrecord. It also retrieves the address subrecord and reads one of its fields. The script logs both values.

This example uses standard mode, but you can also use dynamic mode to load the subrecord. For general details about using either approach to retrieve a sublist subrecord, see Using SuiteScript 2.x to Retrieve a Sublist Subrecord.

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
           });

        // Retrive a value from the sublist line
        var labelValue = rec.getSublistValue({
            sublistId: 'addressbook',
            fieldId: 'label',
            line: 0
        });

        // Retrieve the subrecord associated with that same line.
        var subrec = rec.getSublistSubrecord({
            sublistId: 'addressbook',
            fieldId: 'addressbookaddress',
            line: 0
        });

        // Create a variable to initialize it to the
        // value of the subrecord's city field.
        var cityValue = subrec.getValue({
            fieldId: 'city'
        });

        // Print the retrieved values to the execution log.
        try {
            log.debug({
                title: 'label value',
                details: 'label value: ' + labelValue
            });
             
            log.debug({
                title: 'city value',
                details: 'city value: ' + cityValue
            });
        } catch (e) {
            log.error({
                title: e.name,
                details: e.message
            });
        }
    }
    return {
        afterSubmit: afterSubmit
    };
}); 

        

Related Topics

General Notices