4

I have enabled the State and Country picklists in Salesforce and I was trying to get/create the same kind of country and state picklist with dependency from Contact in a visual force page.

I have done the below steps:

From the controller i tried to create the country and state picklists in the custom visual force page.

Add Contact Visual force Page:

<apex:page StandardController="CustomObject__c" extensions="AddContactController">
<apex:form >
    <apex:pageBlock title="Add Contact" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Details" columns="2">
              <apex:selectList id="states" value="{!myContact.MailingState}" size="1" required="false">
              <apex:selectOptions value="{!states}"/>
            </apex:selectList>
              <apex:selectList id="countries" value="{!myContact.MailingCountry}" size="1" required="true">
              <apex:selectOptions value="{!countries}"/>
            </apex:selectList>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

AddContactController Apex Class

public class AddContactController {
ApexPages.StandardController stdCtrl;
public Contact myContact{ get; private set;}

public AddContactController(ApexPages.StandardController std) {
    stdCtrl = std;
    myContact = new Contact();
}   
public List<SelectOption> countries { get{ return getCountries(); }set; }
public List<SelectOption> states{ get{ return getStates(); }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.getvalue()));
  };
  return options;
}}

The above code populates the entire countries and states in the custom visual force page from the Contact State/Country picklists but doesn't have dependency on the country and state.

Is there any way to get the dependency and by country onchange i could show respective states in custom visual force page instead of manually configuring the dependency or using external packages(Provenworks).

Thanks in advance .

5
  • 1
    Try finding smth here salesforce.stackexchange.com/questions/4462/… Sep 20, 2016 at 7:23
  • @VladyslavKushney, Thanks. But i'm looking for making using of the State and Country picklists into the custom visual force page(Custom Object) and the dependency when the State/ Country picklists are enabled in an Org. Sep 20, 2016 at 11:31
  • Visualforce in not a problem here because you are building custom page, you have to focus on getting dependent values in apex. If you find solution, in visualforce and apex the only thing you need to do is to create functionality for dependent data. But in essence the problem is getting those values in apex Sep 20, 2016 at 13:00
  • @VladyslavKushney, you are right. I was able to get the Countries and States into the apex from the Contact object in the mentioned code but i couldn't find any field which has the dependency relationship. I could only find MailingCountry(String), MailingCountryCode(Picklist) and similarly the MailingState(String), MailingStateCode(Picklist). Is there any way of getting the dependency between them when the state and country picklists are enabled. Sep 20, 2016 at 18:50
  • However there is a problem with list<Schema.PicklistEntry> values = fieldMap.get('MailingCountryCode').getDescribe().getPickListValues(); as this when packed into a Managed Package, it couldn't identify the MailingCountryCode. Nov 17, 2016 at 7:55

3 Answers 3

2

State and Country picklist metadata is only available as of Winter 17 via the MetaData API

Documentation on the metadata API for the object AddressSettings is here.

Apex wrapper for the Metadata API is available on GitHub

Idea to make this more accessible from APEX directly without requrinig a callout can be found here. SFDC has announced they are starting to develop this!

So, you have a bit of work to do to query this and exploit in a VF page

2

Came across this in 2021 and this can indeed be solved with User Interface API. Detailed explanation can be found here - Build UI for Picklists

Here is what we did:

  • get all picklist values for State using /ui-api/object-info/Account/picklist-values/{recordTypeId}/BillingStateCode
  • In the response, the controllerValues has mapping of country code to their corresponding numeric value ex: IN = 102. So lookup with the country code selected by the user and get the corresponding numeric value
  • Then fetch the values from "Values" property where "validFor" = 102. This will give you all the states values applicable for the selected country.

Hope this helps others.

0

Looking into this in April 2018 and it looks like the Picklist Values response available in the User Interface API includes a controllerValues property:

If the picklist is dependent, this property is a map of its immediate controlling field’s picklist values to their indexes.

  • If the controlling field is a picklist, the String is the picklist value and the integer is the value’s index.
  • If the controlling field is a checkbox, the values in the map are "false": 0 and "true": 1.

If the picklist is independent, the map is empty.

Doesn't make too much sense to me on a first reading and there is also a dependentFields property that relates to this. But it looks like the necessary data is there...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .