0

I have enabled State and Country Picklists in my org. I am getting values in piclist. But the are not dependent. Like if i select country "India" i should get states only related to india. But it is not happening so.

<apex:page controller="dependentCountryandState" >
  <apex:form >
  <apex:selectList id="countries" value="{!tmpContact}" size="1" required="true">
  <apex:selectOptions value="{!countries}"/> 
  </apex:selectList>

  <apex:selectList id="states" value="{!tmpState}" size="1" required="true">
  <apex:selectOptions value="{!states}"/> 
  </apex:selectList>
  </apex:form>

public with sharing class dependentCountryandState {

    public String tmpState { get; set; }

    public String tmpContact { get; set; }

     public List<SelectOption> getCountries()
    {
        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.getValue(), v.getLabel()));
        }
        return options;
    }

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

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

}

How to get dependent state for the dependent country code?

  • Do you want to use for contact standard state and country field? – Ratan Paul Jan 15 '16 at 11:27
  • Yes I want to use standard state and country field. – Swapnil Kale Jan 15 '16 at 11:28
  • 1
    Just for reference adding............... In VF use <apex:inputField Value="{!Contact.ContryCode}"> and <apex:inputField Value="{!Contact.StateCode}"> for dependent picklist – Ratan Paul Jan 15 '16 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.