+ Start a Discussion
Kevin Jackson 11Kevin Jackson 11 

How to get countries in a list in APEX

Hi Have scanned the resources and cannot find a solution that works:

I know it has to be done along these lines here:

public static void getCountries()
    {
        Schema.DescribeFieldResult fieldResult = Contact.MailingCountryCode.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry s:ple){
            
            System.debug('value: '+s.getLabel());
        } 
    }

BUT I get this error :  "Method does not exist or incorrect signature: void getDescribe() from the type String"


With VF Page including:

<apex:selectList value="{!Country}" size="1" required="true">
                    <apex:selectOptions value="{!Countriesj}"/>
                      </apex:selectList>

I have tried other code, like the one from @gregorz Skaruz 
 http://skaruz.com/en/2015/03/english-salesforce-how-to-access-state-and-country-picklists-values-in-apex/

It looks great, but is is coming up with an empty Select options List.

Any Help is appreciated.
Amit Chaudhary 8Amit Chaudhary 8
Did you try code like below

  Schema.sObjectType objType = Contact.getSObjectType();
  Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
  map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
  list<Schema.PicklistEntry> values = fieldMap.get('MailingCountryCode').getDescribe().getPickListValues();

  List<SelectOption> options = new List<SelectOption>();
  for (Schema.PicklistEntry v : values){
    options.add(new SelectOption(v.getLabel(), v.getLabel()));
  }

System.debug('---->'+options);