NetSuite provides built-in lists for countries and their corresponding states or provinces. These lists are commonly used in address fields, custom records, and forms where users need to select a country or state.
This article will help you access these lists programmatically in SuiteScript.
The value for the Country field use the ISO 3166-1 alpha-2 code. We can prove it by use the N/search module to get the country list by grouping the state records by country.
var stateSearchObj = search.create({
type: "state",
filters:
[
],
columns:
[
search.createColumn({
name: "country",
summary: "GROUP",
label: "Country"
})
]
});
stateSearchObj.run().each(function(result){
console.log(result.getValue({name:'country',summary:'group'}));
console.log(result.getText({name:'country',summary:'group'}));
return true;
});
For example, the value may be:
AU,CA,CN,JP,MX,GB,US
And correspond text would be:
Australia,Canada,China,Japan,Mexico,United Kingdom,United States
The state record in NetSuite is a record type that represents the states or provinces of a country.
The state record has the following fields: country, full name, short name.
The unique key is combined by the country and state to map to an actual state.
For getting the correct value for the state field, you may need to use the COUNTRY to get the correct state.
For the sales order record, the record exists a field called shippingaddress which is a subrecord. When get country / state value from that subrecord, you would get the country to 2 chars and the state in short name.
const soRec = record.load({
type: record.Type.SALES_ORDER,
id: 1234,
isDynamic: true
});
const shipAddrRec = soRec.getSubrecord('shippingaddress');
const countryNameInStr = shipAddrRec.getValue('country');//US
const stateNameInStr = shipAddrRec.getValue('state');//CA
In this case, if you want to get the internal id of the state to update another record, you may need to use the N/search module to get the internal id of the state.
let stateId = -1;
const stateSearchObj = search.create({
type: "state",
filters: [
["shortname","startswith",stateNameInStr],
"AND",
["country","anyof",countryNameInStr]
],
columns: [
search.createColumn({name: "id", label: "Id"}),
]
});
stateSearchObj.run().each(function(result){
stateId = result.id;
});
//return stateId;
The main concept is “the state record is unique by the country and the short name of the state”. By knowing these, you can use above snippet to get the internal id of a state and update the id to the field you would like to update or use.
This article has provided an overview of how to retrieve state and country lists in NetSuite using SuiteScript.
By understanding the structure of the state and country records, you can effectively access and manipulate this data in your scripts. This knowledge is essential for tasks such as prepopulating dropdown fields, validating addresses, and mapping external data to NetSuite records.
About Us
Concentrus is a leading provider of innovative cloud-based enterprise resource planning (ERP) solutions, including NetSuite. Our team of NetSuite experts offers a range of services, including NetSuite price analysis, NetSuite training, and NetSuite integration services.
Our goal is to help businesses of all sizes maximize their investment in NetSuite by providing expert NetSuite cost optimization and implementation strategies. With years of experience as a NetSuite partner, our NetSuite administrators and NetSuite consultants are well equipped to help businesses of all sizes with their NetSuite consulting needs.
Whether you're looking for a NetSuite consultant to help with your NetSuite implementation or you need ongoing NetSuite support, Concentrus is here to help.
Read About Our Implementation Methodology
Want more NetSuite Tips and Tricks? Check out our Short & 'Suite videos