Salesforce Apex method to get the country name by ISO Code

Convert ISO code into country name in Salesforce Apex


Andres Canavesi
Jul 24, 2022
This method in Apex receives the country ISO code as string and returns the out of the box country name.
featured image
Country ISO code for this method is the alpha-2. Example: UY for Uruguay, AR for Argentina, etc
This method in Apex receives the country ISO code as string and returns the out of the box country name.
      
public static String getCountryNameByIsoCode(String isoCode){
    if(isoCode == null) {
        return null;
    }
    
    Schema.DescribeFieldResult fieldResult = User.Countrycode.getDescribe();
    List 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;
  }

  
    
Photo by Marjan Blan | @marjanblan on Unsplash