When you’re developing applications in Salesforce, you may need to retrieve the name of a country based on its ISO code. If this is the case, let’s implement an Apex method that you can use to simplify the process.
The method is called getCountryNameByIsoCode
and it takes a string argument that represents the ISO code for a particular country. For example, if you want to retrieve the name of the United States you would use the ISO code US.
The country ISO code for this method is from the standard alpha-2. Example: UY for Uruguay, AR for Argentina, etc
public static String getCountryNameByIsoCode(String isoCode){
if(isoCode == 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.getValue().toLowerCase() == isoCode.toLowerCase()) {
return pickListEntry.getLabel();
}
}
return null;
}
One response to “Salesforce Apex method to get the country name by ISO Code”
[…] If you want to get the ISO code by country name, see this post […]