Mapping Country Enum Values in NetSuite

Oh boy, is this the article you’ve been searching across for a while? It doesn’t really surprise me because NetSuite provides very little documentation for webservices integrations. On top of that, NetSuite makes a developers life very easy by its internal id game. So If you’re really interested in mapping country enum values easily, you’ve come to the right place.

Most of NetSuite’s search operations return the internal ids of corresponding objects. For example, if you’re searching for customers, NetSuite will return the country, partner and other values in terms of internal ids. You will have to make another webservices call to map these values.

Why bother with Mapping Country Enum Values?

The main issue with country names is that customer objects have stored enum values (e.g:- “_unitedStates”). But, the getSelectValue operation returns country names in the form (“US”, “United States”) where “US” is the internal Id.

Okay, let’s get into business.

Step 1

You need to retrieve all of the country names and create a mapping between internal Ids and the country names. The following code snippet will create a hash map for you with objects such as (“US”, “United States”).

private Map<String, String> getCountryNamesAsMap() {
    RecordType recordType = RecordType.CUSTOMER;
    String field = "country";
    String sublist = "addressbookList";
    GetSelectValueRequest selectValueRequest = new GetSelectValueRequest();
    GetSelectValueFieldDescription description = new GetSelectValueFieldDescription();
    description.setRecordType(recordType);
    if (sublist != null && !sublist.trim().isEmpty()) {
        description.setSublist(sublist);
    }
    description.setField(field);
    selectValueRequest.setFieldDescription(description);

    Preferences selectPreferences = new Preferences();
    selectPreferences.setIgnoreReadOnlyFields(true);
    GetSelectValueResult getSelectValueResult;
    getSelectValueResult = client.getSelectValue(selectValueRequest, passport, applicationInfo, partnerInfo, selectPreferences, new Holder<>()).getGetSelectValueResult();
    List<BaseRef> baseRefList = getSelectValueResult.getBaseRefList().getBaseRef();

    return baseRefList.stream().map(RecordRef.class::cast).filter(item -> item.getName() != null).collect(Collectors.toMap(RecordRef::getInternalId, RecordRef::getName));
} 

Step 2

Now you need to convert the full names into enum values. Simple, we will try to convert “United States” => “_unitedStates”. The following code snippet will translate a given country name to the enum format.

private String constructCountryEnum(String countryName) {
    HashMap<String, String> nsExceptions = new HashMap<>();
    /* deprecated countries must be handled separately */
    nsExceptions.put("Congo, Democratic Republic of", "_congoDemocraticPeoplesRepublic");
    nsExceptions.put("Myanmar (Burma)", "_myanmar");
    nsExceptions.put("Wallis and Futuna", "_wallisAndFutunaIslands");

    /* Replace 's with s*/
    String tempCountryName = countryName.replaceAll("\'s", "s");
    tempCountryName = Normalizer.normalize(tempCountryName, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
    tempCountryName = tempCountryName.replaceAll("\\W{1,}", " ");
    String[] words = tempCountryName.split("(\\s)|'");

    for (int i = 0; i < words.length; i++) {
        words[i] = i == 0 ? String.valueOf(Character.toLowerCase(words[i].charAt(0)) + words[i].substring(1)) :
                            String.valueOf(Character.toUpperCase(words[i].charAt(0)) + words[i].substring(1));
    }

    String countryEnum = "_" + String.join("", words);
    return countryName.contains("Deprecated") ? "" : nsExceptions.getOrDefault(countryName, countryEnum);
}

Step 3

You need to create a map so that you can easily look up values.

map.forEach((key, name) -> {
    enumMap.put(getCountryEnum(name), name);
});

Step 4

Look up the values, as and when needed.

That’s it!

Hope you enjoyed it. Cheers!