Retrieving a Body Field Address Subrecord Example

Many record types have body fields that reference address subrecords. A similar design exists with the Company Information page, which is accessed at Setup > Company > Company Information. Technically, this page is not a record, but you can interact with it as you would interact with a record. And like a record, the Company Information page includes body fields that reference subrecords. Specifically, it includes a mainaddress, shippingaddress, and returnaddress subrecord. The following example shows how to retrieve values from each of these addresses.

Note also that this page includes body fields labeled state and country. These fields are set independently of the subrecords. In SuiteScript, you interact with them as you would any standard body field.

To get the most from this example, you should first populate the Company Information page with values in the following fields:

For general details on retrieving body field subrecords, see Using SuiteScript 2.x to Update a Body Field Subrecord.

          /**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define (['N/config', 'N/record'],function(config, record) {
    function afterSubmit(context) {
        // The Company Information page is not a standard record. You access it by 
        // using the config.load method.

        var companyInfo = config.load({
            type: config.Type.COMPANY_INFORMATION
        });

        // Retrieve some of the preferences stored on the main part of the 
        // Company Information page. You interact with these preferences as if
        // they were body fields.

        var companyName = companyInfo.getValue({
            fieldId: 'companyname'
        });

        var employerId = companyInfo.getValue({
            fieldId: 'employerid'
        });

        // The Company Information page also includes some address fields on 
        // the body of the record. Retrieve these fields. 
        var state = companyInfo.getValue({
            fieldId: 'state'
        });

        var country = companyInfo.getValue({
            fieldId: 'country'
        }); 

        // Retrieve details from the subrecord that represents the main address. 
        // As a first step, instantiate the subrecord.
        var mainAddress = companyInfo.getSubrecord ({
            fieldId: 'mainaddress'
        });

        // Retrieve values from the main address subrecord.
        var mainAddressCity = mainAddress.getValue({
            fieldId: 'city'
        });

        var mainAddressState = mainAddress.getValue({
            fieldId: 'state'
        });

        // Retrieve details from the subrecord that represents the shipping address.
        // To start, instantiate the subrecord.   
        var shippingAddress = companyInfo.getSubrecord('shippingaddress');

        // Retrieve values from the subrecord.
          var shippingAddressCity = shippingAddress.getValue({
            fieldId: 'city'
        });

        var shippingAddressState = shippingAddress.getValue({
            fieldId: 'state'
        });

        // Retrieve details from the subrecord that represents the return address.
        // To start, instantiate the shipping address subrecord.   
          var returnAddress = companyInfo.getSubrecord('returnaddress');

        // Retrieve values from the subrecord.
        var returnAddressCity = returnAddress.getValue({
            fieldId: 'city'
        });   
   
        var returnAddressState = returnAddress.getValue({
            fieldId: 'state'
        });
   
          // Write selected details to the log.
          log.debug ({
            title: 'mainAddressState',
            details: mainAddressState
        });
   
        log.debug ({
            title: 'shippingAddressState',
            details: shippingAddressState
        });
   
        log.debug ({
            title: 'returnAddressState',
               details: returnAddressState
        });
       }
       return {   
        afterSubmit: afterSubmit
    };
}); 

        

Related Topics

Using SuiteScript 2.x to Retrieve a Body Field Subrecord

General Notices