When it comes to Salesforce development, there are various tools and techniques that can help make the development process more efficient. One such technique is using an Apex method to get the ISO code by country name. By using an Apex method, you can quickly and easily obtain the required ISO code based on the country name.
An ISO code is an international standard used to identify a specific country, region, province, or language. For example, each country in the world is assigned a unique ISO code, such as the United States (USA
An ISO code is an international standard used to identify a specific country, region, province, or language. For example, each country in the world is assigned a unique ISO code, such as the United States (USA
Salesforce Apex provides developers with the ability to retrieve ISO Code information for specific countries. This is useful for international applications where a user’s country or language needs to be stored. To prevent confusion, ISO Codes are often used in such cases.
There are several ways to create an Apex method to retrieve an ISO Code. Let’s see one of them.
If you want to get the ISO code by country name, see this post
public static String getIsoCodeByCountryName(String countryName) {
if (countryName == null) return null;
Schema.DescribeFieldResult fieldResult = User.Countrycode.getDescribe();
List<Schema.PicklistEntry> pickListValues = fieldResult.getPicklistValues();
for (Schema.PicklistEntry pickListEntry : pickListValues) {
// pickListEntry.getLabel() returns the country name
// pickListEntry.getValue() returns the country code
if (pickListEntry.getLabel().toLowerCase() == countryName.toLowerCase()) {
return pickListEntry.getValue();
}
}
return null;
}
How to test the Apex method getIsoCodeByCountryName
@isTest static void testGetIsoCodeByCountryNameDynamic() {
// given
List<String> countries = getCountries();
for(String country :countries){
// when
String isoCode = getIsoCodeByCountryName(country);
// then
System.assert(isoCode != null);
}
}
@isTest static void testGetIsoCodeByCountryNameInvalid() {
// given
String country = 'invalid country';
// when
String isoCode = getIsoCodeByCountryName(country);
// then
System.assert(isoCode == null);
}
@isTest static void testGetIsoCodeByCountryNameNull() {
// given
String country = null;
// when
String isoCode = getIsoCodeByCountryName(country);
// then
System.assert(isoCode == null);
}
See that one of our test cases uses one aux method called getCountries to get all the countries available in the your Salesforce instance
public static List<String> getCountries(){
Schema.sObjectType objType = Contact.getSObjectType();
Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
list<Schema.PicklistEntry> values = fieldMap.get('MailingCountryCode').getDescribe().getPickListValues();
List<String> countries = new List<String>();
for (Schema.PicklistEntry v : values){
countries.add(v.getLabel());
}
return countries;
}