function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gaurav.sfdcgaurav.sfdc 

dependent behavior of Country and State picklist in Apex code

How to implement the dependent behavior of this new feature (i.e. enabling Country and State picklist in Org). On standard contact page it shows depent behavior automatically but when we bind these field with in VF page the dependent behavior is lost

Is there a simple way to implement this behavior in VF page

Any help would be appreciated
 
Arunkumar RArunkumar R
Hi Gaurav,

If you wanna picklist list behaviour on Address field in visualforce page, then you need to use CountryCode and StateCode fields,

Referring Contact Mailing Address:
 
<apex:page standardController="Contact">
    
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.MailingCountryCode}"/>
                <apex:inputField value="{!Contact.MailingStateCode}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Referring Lead Address:
 
<apex:page standardController="Lead">
    
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection >
                <apex:inputField value="{!Lead.CountryCode}"/>
                <apex:inputField value="{!Lead.StateCode}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>


Referring Billing Account Address:

 
<apex:page standardController="Account">
    
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection >
                <apex:inputField value="{!Account.BillingCountryCode}"/>
                <apex:inputField value="{!Account.BillingStateCode}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>


Hope you are clear..!  Refer more at details about address field at 
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/compound_fields_address.htm
Akram GargouriAkram Gargouri
There is no standard way to have that behaviour on a VF page, the only thing is you have a standard validation of the values.

Since counties/states codes are an ISO-3166 standard, you can implement your cusom select inputs based on that.

https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_standard_countries.htm&language=en_US
gaurav.sfdcgaurav.sfdc
Thanks Ankur and Akram,

@Ankur your code will crash if you disable 'Country and State Picklist' 
@ Akram, not sure what you are are trying to say but I figured out how to do it. These two links helped me

First I used describe call to check if MailingCountryCode/StateCode exists in Org, If exist the I used dynamic binding of object and Field

You need to do little modification of below links to make it work for you, Let me know if you want my modified code as well

http://salesforce.stackexchange.com/questions/48920/dynamic-picklists-of-objects-its-fields

http://forceschool.blogspot.in/2011/06/dynamic-binding-when-object-name-is.html




 
Arunkumar RArunkumar R
Hi Gaurav,

As per salesforce document, the below statement is mentioned,

StateCode and CountryCode are always available on compound address fields, whether or not state and country picklists are enabled in your organization.

However, it's not accessible if the state and country picklist options are disabled.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/compound_fields_address.htm


Mark this as a solution if this helpful.
gaurav.sfdcgaurav.sfdc
Yes Ankur, This was my question that how to handle the code in those Orgs where it is disabled. Actually we have a managed package. If we install it in an Org where it is disabled then it will crash so we needed some dynamic way. The solution which I have shared works on both orgs where this feature is enabled/disabled
 
satish kota 8satish kota 8
Hello Gaurav, Can you share your code getting dynamic values of State when a country is selected. I am searching a lot for this.