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
DILEEP NALLADILEEP NALLA 

Dependent Drop down list is not working

Hi,

I have a requirement of filtering drop down list based on the another drop down list

I have two custom settings countries and states, based on countries filter I have to filter states.
Here is my code:
public with sharing class CountryStatePicker {
    public Apexpages.StandardController stndctrl;
    Public Custom_Info__c CSP {get;set;}
     Public CountryStatePicker(Apexpages.StandardController stndctrl)
    {
        CSP = (Custom_Info__c)stndctrl.getrecord();
        this.stndctrl= stndctrl;
    }
    
    
  // Variables to store country and state selected by user 
    Public string state{get;set;}
    Public string country{get;set;}
    
  // Generates country dropdown from country settings
    public List<SelectOption> getCountriesSelectList()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--Select Countries--'));
    
   // Find all the countries in the custom setting
    Map<String, Foundation_Countries__c> countries = Foundation_Countries__c.getAll();
    
   // Sort them by name
    List<String> countryNames = new List<String>();
    countryNames.addAll(countries.keySet());
    countryNames.sort();
   // Create the Select Options.
    for (string countryName : countryNames ){
        Foundation_countries__c Country = countries.get(countryName);
        options.add(new SelectOption(country.country_code__c, country.Name));
    }
    
    return options;
}
    // To generate the states picklist based on the country selected by user.

    public List<SelectOption> getStatesSelectList() {

        List<SelectOption> options = new List<SelectOption>();

        // Find all the states we have in custom settings.
        Map<String, Foundation_States__c> allstates = Foundation_States__c.getAll();

        // Filter states that belong to the selected country
        Map<String, Foundation_States__c> states = new Map<String, Foundation_States__c>();
        for(Foundation_States__c state : allstates.values()) {
            if (state.country_code__c == this.country) {
                states.put(state.name, state);
            }
        }
        // Sort the states based on their names
        List<String> stateNames = new List<String>();
        stateNames.addAll(states.keySet());
        stateNames.sort();
        // Generate the Select Options based on the final sorted list
        for (String stateName : stateNames) {
            Foundation_States__c state = states.get(stateName);
            options.add(new SelectOption(state.state_code__c, state.state_name__c));
        }
       // If no states are found, just say not required in the dropdown.
        if (options.size() > 0) {
            options.add(0, new SelectOption('', '-- Select One --'));
        } else {
            options.add(new SelectOption('', 'Not Required'));
        }
        return options;
    }
}

-------------

My Vf Page code:
<apex:page standardController="Custom_Info__c" extensions="CountryStatePicker">
   <apex:form >
      <apex:actionFunction name="rerenderStates" rerender="statesSelectList" >
          <apex:param name="firstParam" assignTo="{!country}" value="" />
      </apex:actionFunction>

       <apex:pageBlock>
          <apex:pageBlockSection columns="2" showHeader="true" title="Application Information Section" collapsible="true"> 
                  <apex:selectList size="1" value="{!country}" id="country" onChange="rerenderStates(this.value)" >
                    <apex:selectOptions value="{!countriesSelectList}"/>
                     <apex:actionSupport event="rerenderStates(this.value)"/>
                 </apex:selectList>
            <apex:selectList size="1" value="{!country}" id="statesSelectList" tabindex="1" >
                <apex:selectOptions value="{!statesSelectList}"/>
                 </apex:selectList>
          </apex:pageBlockSection>  
      
      </apex:pageBlock>  
    <apex:inputField id="description" value="{!CSP.Describe_Our_State__c}" required="true" taborderhint="2" />
       <apex:pageBlock >
        <apex:commandButton value="Save" action="{!Save}" tabindex="8" />   
       </apex:pageBlock>     
    </apex:form>
    
</apex:page>