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. Although it's not a record, you can interact with it like one. Like a record, it has body fields that reference subrecords. It includes mainaddress, shippingaddress, and returnaddress subrecords. The following example shows how to retrieve values from each of these addresses.
Note that this page also has state and country fields. These fields are set separately from the subrecords. In SuiteScript, you interact with them like any standard body field.
To get the most out of this example, first populate the Company Information page with values in these fields:
-
State
-
County
-
Address
-
Shipping address
-
Return address
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
};
});