Tuesday 15 February 2022

How to get all country code and state code thorough Apex ?

 If we need to get state name or country from the code selected from country and state picklist, here is the sample code.

To access 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());

}

To access all the state code 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());

}

No comments: