+ Start a Discussion
lavanya gottumukkalalavanya gottumukkala 

Restricting picklist values through apex and visualforce

Hi All,

I have the below requirement.
On registration page we have two picklist fields 'country' and 'Taxes'.By default when page is loaded first time,I want to set country as 'India' and Taxes as 'PAN'.But once user changed the country from India to any other,then I would like to display the Taxes related to that country only(through select list).How it can be achieved through actionfunction?

Thanks in advance.
jyothsna reddy 5jyothsna reddy 5
Hi Lavanya,

Please try this below sample code.
VisualForce Page:
<apex:page controller="Task23Apex">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputLabel >State</apex:outputLabel>
            <apex:pageblockSectionItem >                
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
            <apex:outputLabel value="City"/>       
            <apex:pageblockSectionItem >
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>

public class Task23Apex {

    public String state { get; set; }
    public String city { get; set; }
    public List<SelectOption> getStates() {
        List<SelectOption> stOptions = new List<SelectOption>();
        stOptions.add(new SelectOption('None','--None--'));
        stOptions.add(new SelectOption('AP','Andhra Pradesh'));
        stOptions.add(new SelectOption('TN','Tamil Nadu'));
        stOptions.add(new SelectOption('KA','Karnataka'));
        return stOptions;

    public List<SelectOption> getCities() {
        List<SelectOption> ciOptions = new List<SelectOption>();
            ciOptions.add(new SelectOption('VSKP','Vishakapatnam'));
            ciOptions.add(new SelectOption('BZA','Vijayawada'));
            ciOptions.add(new SelectOption('TPTY','Tirupati'));
        else if(state=='TN'){
            ciOptions.add(new SelectOption('CHE','Chennai'));
            ciOptions.add(new SelectOption('CBE','Coimbatore'));
        else if(state=='KA'){
            ciOptions.add(new SelectOption('SBC','Bangalore'));
            ciOptions.add(new SelectOption('MAQ','Mangalore'));
        else if(state=='None'){
            ciOptions.add(new SelectOption('None','--None--'));
        return ciOptions;


Replace cities with taxes and city names with tax names.

Hope it helps you.

Jyothsna D.
Ruwantha  LankathilakaRuwantha Lankathilaka

Write a method which returns select option list based when pass the country. Then rerender the tax list based on the parsed country. The code is more like a guidance and let me know if you have any problem.

In the controller do something like this

public String selectedCountry{get;set;}
public String selectedTax {get;set;}
private list<SelectOption> countryTax(String countryName){

// implement the logic that returns the list of selectOptions based on country

public list<SelectOption> getTaxes(){

if(selectedCountry != null){
  return  this.countryTax(selectedCountry);
// give error message
return null;

public list<SelectOption> getCountries{
// return the country option list

In page you can do

            <apex:selectList id="fld1" value="{!selectedCountry}" multiselect="false" size="1" >                                            

              <apex:selectOptions value="{!Countries}"/>       
                     <apex:actionSupport event="onselect" action="{!Taxes} rerender="taxPanel
                          " status="SearchStatus immediate="true"/>                                                      

 <apex:outputpanel id="taxPanel">
            <apex:selectList id="fld1" value="{!selectedTax }" multiselect="false" size="1" >                                            

              <apex:selectOptions value="{!Taxes}"/>