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
MMuneeeMMuneee 

Dependent picklist using Visualforce

I have a two picklist values.
1.Country  2.State
if i select the india then the  state picklist shoud have states belongs to india.Like that Us also

Can Any one help me.If possible provide the code

Thanks 
Best Answer chosen by MMuneee
Frédéric TrébuchetFrédéric Trébuchet
Hi,

Here is a sample.
First, define your Visulforce Page with the picklists.
You can see that it uses a custom controller and an event "onchange" on country list to change the content of the states list when you select a country. It looks like this :
<apex:page controller="ctlDepPickLst">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="Country"/>
                </apex:pageblockSectionItem>        
                <apex:pageblockSectionItem>                
                    <apex:selectList size="1" value="{!country}">
                        <apex:selectOptions value="{!countries}"/>
                        <apex:actionSupport event="onchange" reRender="a"/>
                    </apex:selectList>                
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="State"/>
                </apex:pageblockSectionItem>            
                <apex:pageblockSectionItem>
                    <apex:selectList size="1" value="{!state}" id="a">
                        <apex:selectOptions value="{!states}"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>            
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>
Then define the APEX Class ctlDepPickLst to populates the options depending on the selected country :
public class ctlDepPickLst {
    public String country {get;set;}
    public String state {get;set;}

    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- Select ---'));        
        options.add(new SelectOption('US','United States'));
        options.add(new SelectOption('IN','India'));
        return options;
    } 
    
    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(country == 'US')
        {       
            options.add(new SelectOption('CO','Colorado'));
            options.add(new SelectOption('NE','Nevada'));
            options.add(new SelectOption('TE','Texas'));
        }
        else if(country == 'IN')
        {       
            options.add(new SelectOption('BI','Bihar'));
            options.add(new SelectOption('KE','Kerala'));
            options.add(new SelectOption('MA','Manipur'));
        }
        else
        {
            options.add(new SelectOption('None','--- Select ---'));
        }      
        return options;
    }       
}
Of course countries and states should come from corresponding objects.
That's all.

Hope this helps.
Fred
 

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi,

Here is a sample.
First, define your Visulforce Page with the picklists.
You can see that it uses a custom controller and an event "onchange" on country list to change the content of the states list when you select a country. It looks like this :
<apex:page controller="ctlDepPickLst">
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection columns="2">
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="Country"/>
                </apex:pageblockSectionItem>        
                <apex:pageblockSectionItem>                
                    <apex:selectList size="1" value="{!country}">
                        <apex:selectOptions value="{!countries}"/>
                        <apex:actionSupport event="onchange" reRender="a"/>
                    </apex:selectList>                
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem>
                    <apex:outputLabel value="State"/>
                </apex:pageblockSectionItem>            
                <apex:pageblockSectionItem>
                    <apex:selectList size="1" value="{!state}" id="a">
                        <apex:selectOptions value="{!states}"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>            
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>
Then define the APEX Class ctlDepPickLst to populates the options depending on the selected country :
public class ctlDepPickLst {
    public String country {get;set;}
    public String state {get;set;}

    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- Select ---'));        
        options.add(new SelectOption('US','United States'));
        options.add(new SelectOption('IN','India'));
        return options;
    } 
    
    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(country == 'US')
        {       
            options.add(new SelectOption('CO','Colorado'));
            options.add(new SelectOption('NE','Nevada'));
            options.add(new SelectOption('TE','Texas'));
        }
        else if(country == 'IN')
        {       
            options.add(new SelectOption('BI','Bihar'));
            options.add(new SelectOption('KE','Kerala'));
            options.add(new SelectOption('MA','Manipur'));
        }
        else
        {
            options.add(new SelectOption('None','--- Select ---'));
        }      
        return options;
    }       
}
Of course countries and states should come from corresponding objects.
That's all.

Hope this helps.
Fred
 
This was selected as the best answer
Cory CowgillCory Cowgill
Have you tried using State and Country Picklists from Out of the Box? If this is a standard Account or Contact Object you can do this without much work.

https://help.salesforce.com/apex/HTViewHelpDoc?id=admin_state_country_picklists_overview.htm&language=en_US

Then you just reference MailingStateCode and MailingCountryCode fields as <apex:inputFields> and it will render choices for you.
Frédéric TrébuchetFrédéric Trébuchet
Hi,
Seems a good solution if the problem is with countries and states, but if you have to deal with other relations of this kind (let's say states and towns, town and schools and so on), knowing how to code it can be usefull.

Regards,
Fred
jaw999jaw999
THis is great. How would I use this as a controller extension for a standard object?
Soundar Rajan PonpandiSoundar Rajan Ponpandi
Dear Frédéric Trébuchet,

It's Really Helpfull


Regards,
Soundar.