Creating an Address on a Subsidiary Record Example

The following example shows how to create a subsidiary record that includes an address. In this case, the address data is contained in an address subrecord assigned to the mainaddress field.

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

This example uses dynamic mode, but you could also add the subrecord using standard mode. For general details about using either approach, see Using SuiteScript 2.x to Create a Body Field 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) {
        var 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.
        var 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 {
            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 Body Field Subrecord
Creating an Inventory Detail Subrecord on a Body Field Example

General Notices