Loading

Access the state and country picklist through Apex

Fecha de publicación: Oct 13, 2022
Descripción
This article gives you sample code to access the state and country pick-list values:
Solución
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 ORG. 
 
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
Número del artículo de conocimiento

000387645

 
Cargando
Salesforce Help | Article