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 could also add the subrecord using standard mode. For general details about using either approach to add a sublist subrecord, see Using SuiteScript 2.x to Create a Subrecord 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){

        // Create the record.
        var 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.
        var 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{
            var recId = rec.save();

            log.debug({
                title: 'Record created successfully',
                details: 'Id: ' + recId
            });
        } catch (e){
            log.error({
                title: e.name,
                details: e.message
            });
        }
    }
    return {
        afterSubmit: afterSubmit
    };
}); 

        

Related Topics

Using SuiteScript 2.x to Create a Subrecord in a Sublist Field
Creating an Inventory Detail Sublist Subrecord Example
Creating an Order Schedule Sublist Subrecord Example
Creating a Landed Cost Sublist Subrecord Example

General Notices