0

I have a picklist which holds the country names and based on the name selected I want to populate the states. I have given a piece of code done by me **but I am not able to get it working for the states picklist

Controller:

public String fanCountry_Region { get; set; }
public List<SelectOption> StateCodeList=new List<SelectOption>();
public String selectedCountryCode {get; set;}
public void fetchfanvalues() {
    fan = new fan__c();
    fan=[SELECT id, Email__c,state__c
         FROM fan__c
         WHERE Encrypted_ID__c=:encryptedfanID];
    if (fan != NULL)
    {
        selectedCountryCode=fan.Country_Region__c; //get the selected code
        system.debug('selectedCountryCode Is :'+selectedCountryCode);41
    }
}
public  List<SelectOption> getCountryCodes() {
    if(countryCodeList.isEmpty()) {
        countryCodeList=new List<SelectOption>();
        List<Countries__c> allCountries = new List<Countries__c>();
        allCountries = [SELECT  Name,CountryCode__c
                        FROM    Countries__c];
        allCountries.sort();

        List<Countries__c> cList=new List<Countries__c>([select CountryCode__c 
                                                         from Countries__c 
                                                         where 
                                                         CountryCode__c=:selectedCountryCode]);

        if (cList.size() > 0) {            
            for(Countries__c country : allCountries) {                
                countryCodeList.add( new SelectOption( country.CountryCode__c, country.Name ) );
            }
        } else {
            countryCodeList.add(new SelectOption('--Select--', '--Select--'));            
        }
    }     
    return countryCodeList;
}
public List<SelectOption> getStateCodeList() {
 for(States__c state :States__c.getAll().values()) {
  if(state.CountryCode__c == selectedCountryCode) {
  stateCodeList.add(new SelectOption(state.CountryCode__c, state.StateCode__c));
  }
 }
return StateCodeList; 
}

VF Page:

<apex:selectList value="{!fanCountry_Region}" size="1">
    <apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
    <apex:selectOptions value="{!countryCodes}"></apex:selectOptions>
</apex:selectList>

<apex:selectList value="{!selectedState}" size="1">
    <apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
    <apex:selectOptions value="{!StateCodeList}"></apex:selectOptions>
</apex:selectList>
4
  • Add code for GetStateCodeList Jun 20, 2017 at 5:49
  • Why don't you use dependent picklist and do mapping Jun 20, 2017 at 5:50
  • @night crawler, public List<SelectOption> getStateCodeList() { for(States__c state :States__c.getAll().values()) { if(state.CountryCode__c == selectedCountryCode) { stateCodeList.add(new SelectOption(state.CountryCode__c, state.StateCode__c)); }} return StateCodeList; } In backend, in the field country_region__c the country code is stored for ex. CA if picklist value is Australia. so based on this code I need to fetch the relevant states. Jun 20, 2017 at 6:01
  • The states__c object also has fields called countryCode__c and stateCode__c. for ex: "Australia" "Australia" "CA", so if the value of selectedcountrycode matches with the value of countryCode__c then I fetch the states. I cannot use a dependent picklist becas I cannot modify the object design. Jun 20, 2017 at 6:28

3 Answers 3

0

1.First create custom setting name: Countries Setting Type:List 2.Create custom setting name: States Setting Type:List 3.In states custom setting create new custom field country__c click on manage and create record by click on new like: enter image description here

VF Code:-

<apex:page controller="DependentListDemo">
<apex:form >
    <apex:actionFunction name="rerenderStates" rerender="stateList" action="{!StateCodeList}">
          <apex:param name="firstParam" assignTo="{!fanCountry_Region}" value="" />
      </apex:actionFunction>
<apex:selectList size="1" onchange="rerenderStates(this.value);">
    <apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
    <apex:selectOptions value="{!countryCodes}"></apex:selectOptions>
</apex:selectList>

<apex:selectList size="1" id="stateList">
    <apex:selectOption itemValue=" " itemLabel="--Select--"></apex:selectOption>
    <apex:selectOptions value="{!StateCodeList}"></apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>

Controller code:-

public class DependentListDemo{
public String fanCountry_Region { get; set; }
public List<SelectOption> StateCodeList{ get; set;}
public map<string,list<String>> contryVsStatemap;

public  List<SelectOption> getCountryCodes() {

    contryVsStatemap = new  map<string,list<String>>();
    List<SelectOption> countryCodeList=new List<SelectOption>();
    map<String, States__c> statemap = new map<String, States__c>();
    map<string, Countries__c> mapCountries = Countries__c.getAll();
    for( string str : mapCountries.keySet()){

        countryCodeList.add(new selectoption(str,str));
    }
    statemap = States__c.getAll();
    for(States__c state : statemap.values()){

        if(contryVsStatemap.containsKey(state.country__c)){

            contryVsStatemap.get(state.country__c).add(state.Name);
        }
        else{

            contryVsStatemap.put(state.country__c, new list<string>{state.Name});
        }
    }
    return countryCodeList;
}
public void StateCodeList() {

    StateCodeList = new List<SelectOption>();

    for(string stateCode : contryVsStatemap.get(fanCountry_Region)){

        StateCodeList.add(new SelectOption(stateCode,stateCode));
    }
}
}
1
<apex:page controller="customcontroller">
<apex:form >
  <apex:actionFunction name="rerenderCity" rerender="citySelectList" >
 <apex:param name="firstParam" assignTo="{!country}" value="" />
 </apex:actionFunction>
 <table><tbody>
  <tr>
    <th>Country</th>
 <td>
         <apex:selectList id="country" styleclass="std" size="1" 
            value="{!country}" onChange="rerenderCity(this.value)">
                <apex:selectOptions value="{!countriesSelectList}"/>
         </apex:selectList>
      </td>
  </tr>
 <tr id="city_input">
    <th>City</th>
      <td>
        <apex:selectList id="citySelectList" styleclass="std" size="1" 
             value="{!city}">
               <apex:selectOptions value="{!citySelectList}"/>
        </apex:selectList>
      </td>
  </tr>

 </apex:form>
</apex:page>
public  class customcontroller {


public String city { 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 One --'));        

    // Find all the countries in the custom setting
    Map<String, Country__c> countries = Country__c.getAll();




    // Create the Select Options.
   for (String countryName : countries.keySet()) {
        Country__c country = countries.get(countryName);
        options.add(new SelectOption(country.Name, country.Name));
    }
    return options;
}

// To generate the states picklist based on the country selected by user.
public List<SelectOption> getcitySelectList() {
    List<SelectOption> options = new List<SelectOption>();
    // Find all the city we have in custom settings.
    Map<String, City__c> allcity = City__c.getAll();

    // Filter city that belong to the selected country
    Map<String, City__c> cities = new Map<String, City__c>();
    for(City__c city : allcity.values()) {
        if (city.Country__c == this.country) {
            cities.put(city.name, city);
        }
    }




    for (String cityName : cities.keySet()) {
        City__c city = cities.get(cityName);
        options.add(new SelectOption(city.Name, city.Name));
    }


    if (options.size() > 0) {
        options.add(0, new SelectOption('', '-- Select One --'));
    } else {
        options.add(new SelectOption('', 'Not Required'));
    }
    return options;
}

}

0

Use below code to get value from selected picklist

<apex:actionFunction name="displayState" rerender="StatesListId" action="{!showStates}">



public String fanCountry_Region { get; set; } public String fanState { get; set; } public List statesList {get;set;} public List countriesList { set; get{

        List<selectOption> countriesList = new List<selectOption>();
        countriesList.add(new selectOption(' ','--Select--'));

        map<string,Countries__c> countryMap = Countries__c.getAll();
        List<string> countries = new List<string>();
        countries.addAll(countryMap.keySet());
        countries.sort();

        for(string country : countries){

            Countries__c c = countryMap.get(country);
            countriesList.add(new selectOption(c.CountryCode__c, c.Name));
        }

        return countriesList; 
    } 
}

public void showStates() {

    statesList = new List<selectOption>(); 
    Map<String, States__c> allstates = States__c.getAll();
    Map<string, States__c> states = new Map<string, States__c>();
    for(States__c state : allstates.values()) { 
        if (state.countryCode__c == fanCountry_Region) { 
            states.put(state.name, state); 
        } 
    }
     if(fanCountry_Region.contains('United States') || fanCountry_Region.contains('Canada ') ){

        statesList = new List<selectOption>();
     }else{

        for (String stateName : stateNames) {
            States__c state = states.get(stateName);
            statesList.add(new SelectOption(state.StateCode__c, state.Name));
        }
     }
}

Thank you Amol Salve Salesforce Developer

You must log in to answer this question.

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