Salesforce allows administrators to enable State and Country/Territory picklists to standardize address data entry across the org. Once enabled, these picklists can be accessed programmatically via Apex using the Schema.DescribeFieldResult class. This article provides sample Apex code to retrieve all country codes and state codes currently available in your Salesforce org, which can be used for custom validation, integration field mapping, or dynamic UI population.
Before running the sample code below, ensure that State and Country/Territory Picklists are enabled in your Salesforce org. Navigate to the following path to enable them and complete all required steps.
Makes sure the state and country picklist is enabled in the ORG.
Schema.DescribeFieldResult fieldResult = User.Countrycode.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
System.debug('Picklist::'+ple);
for( Schema.PicklistEntry f : ple){
System.debug(f.getLabel() +'::'+ f.getValue());
}
Schema.DescribeFieldResult fieldResult = User.statecode.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
System.debug('Picklist::'+ple);
for( Schema.PicklistEntry f : ple){
System.debug(f.getLabel() +'::'+ f.getValue());
}
Important: The code samples above return all available picklist entries for the country and state fields respectively. To filter by a specific country, or to retrieve state values for a particular country code only, additional filtering logic is required. These samples serve as a starting point and must be adapted to meet your specific business requirements.
000387645

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.