map of the world book laid open on brown wooden surface

How To Access Country & State Picklist Values In Apex

Description

This article gives you sample code to access the state and country pick-list values if it is enabled in your Salesforce org.

Solution

Makes sure the state and country picklist is enabled in the ORG.

  • For Lightning UI : “Setup | Data | State and Country/Territory Picklists | Complete all the steps” 
  • For Classic: “Setup | Data Management | State and Country/Territory Picklists | Complete all the steps “
  • Find below the link that has steps to enable the state and country pick list in the Salesforce org. 

 1. Following is the sample code for accessing all the country codes:

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());
}

2. Following is the sample code for accessing all the state codes:

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());
}

Please note that this is just the sample code and will not fulfill the entire business requirement. In case of any implementation support or any other help, please contact us here: contact@tenetizer.com.