Creating an Address on a Subsidiary Record Example

The following example shows how to create a subsidiary record that includes an address. The address data is in an address subrecord linked to the mainaddress field.

To use this example, you must meet the following prerequisites:

This example uses dynamic mode, but you can also use standard mode to add the subrecord. For general details about using either approach, see Creating a Body Field 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) => {
        const rec = record.create({
            type: record.Type.SUBSIDIARY,
            isDynamic: true
          });

        // Set body fields on the record.
        rec.setValue({
            fieldId: 'name',
            value: 'US Subsidiary'
        });

        rec.setValue({
            fieldId: 'state',
            value: 'CA'
        });

        // Create the address subrecord.
        const subrec = rec.getSubrecord({
            fieldId: 'mainaddress',
        });

        subrec.setValue({
            fieldId: 'city',
            value: 'San Mateo'
        });

        subrec.setValue({
            fieldId: 'state',
            value: 'CA'
        });

        subrec.setValue({
            fieldId: 'zip',
            value: '94403-2511'
        });

        subrec.setValue({
            fieldId: 'addr1',
            value: '2955 Campus Drive'
        });

       subrec.setValue({
            fieldId: 'addr2',
            value: 'Suite 100'
        });

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

        

Related Topics

General Notices