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 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

General Notices