Print this page

How to access the state and country picklist through Apex

Knowledge Article Number 000212327
Description This article gives you sample code to access the state and country pick-list values:
Resolution Makes sure the state and country pick-list is enabled in the ORG.
  • Setup->Data Management->State and Country Picklists -> Complete all the steps 
  • Find below the link that has steps to enable the state and country pick list in the ORG. 
  • https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_overview.htm 

1) Following is the sample code for accessing all the country codes 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());
}



2) Following is the sample code for accessing all the state codes in the ORG
 
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




promote demote