1

On select of particular region the countries under that specific region has to display under country picklist.I have tried doing that but in country picklist i am not getting any values.Below is the apex class and visualforce.

VisualForce

 <apex:page Controller="testing_custom">
   <apex:form >
     <apex:selectList label="Region" value="{!region}" size="1" multiselect="false">
      <apex:selectOptions value="{!regionsSelectList}"/>
      <apex:actionSupport event="onchange" action="{!fetchCountry}" rerender="contrySelect"/> 
     </apex:selectList>
  <apex:outputPanel id="contrySelect">Country
         <apex:selectList label="Country" value="{!country}" size="1" multiselect="false" >
         <apex:selectOptions value="{!lstCountriesSelectList}"/>
        </apex:selectList>

        </apex:outputPanel>

   </apex:form>
  </apex:page>

apex

  public class testing_custom
 {
   public String region{get;set; }
   public String country{get;set;}
    public List<SelectOption> lstCountriesSelectList {get;set;}
    public testing_custom{
    lstCountriesSelectList = new List<SelectOption>();
    }
  //For getting region list from custom settings object
    public List<SelectOption> getregionsSelectList(){ 
    List<SelectOption> options = new List<SelectOption>(); 
   set<String> setUniqueRegion = new set<String>();
   for(OppCountryRegionMapping__c setting :         OppCountryRegionMapping__c.getAll().values()){
   if(!setUniqueRegion.contains(setting.Region__c)){
    options.add(new SelectOption(setting.Region__c, setting.Region__c));
    setUniqueRegion.add(setting.Region__c);       
   }
  }    

    return options;
  }

     //For getting country list based on selected region from custom settings object
    public List<SelectOption>  getCountriesSelectList() {

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

     for(OppCountryRegionMapping__c setting : OppCountryRegionMapping__c.getAll().values()) {

      if (setting.Region__c== this.region) {
        options.add(new SelectOption(setting.Region__c,setting.Country__c));

        }

      }
       system.debug('*****selected value inside'+options);
       return options;
      }

Thanks

When i click on region ASIA,its displaying the corresponding country value.Below is the screenshot.

enter image description here

When i click on another region LATAM the corresponding country is appending with the ASIA region's country values.I have encircled the value which is appending.Below is the screenshot enter image description here

When the page loads for the first time

enter image description here

2 Answers 2

0

try with this.. In your controller you are giving select option value = name

and comparing in another method Region ... that is the error

So now assign selectoption value to region and compare with that in another method

also you need to add action support so when you change the region then controller method should called

<apex:page Controller="testing_custom">
   <apex:form >
     <apex:selectList label="Region" value="{!region}" size="1" multiselect="false">
      <apex:selectOptions value="{!regionsSelectList}"/>
      <apex:actionSupport event="onchange" action="{!fetchCountry}" rerender="contrySelect"/>
     </apex:selectList>
     <apex:outputPanel id="contrySelect">
         <apex:selectList label="Country" value="{!country}" size="1" multiselect="false" >
      <apex:selectOptions value="{!lstCountriesSelectList}"/>
     </apex:selectList>

     </apex:outputPanel>

   </apex:form>
  </apex:page>

Controller

 public class testing_custom
 {
   public String region{get;set; }
   public String country{get;set;}
   public List<SelectOption> lstCountriesSelectList {get; private set;}
   public testing_custom(){
    lstCountriesSelectList=new  List<SelectOption>();
    if(OppCountryRegionMapping__c.getAll().values().size()> 0)
        region = OppCountryRegionMapping__c.getAll().values()[0].Region__c;
    fetchCountry();
  }

  //For getting region list from custom settings object
    public List<SelectOption> getregionsSelectList(){ 
    List<SelectOption> options = new List<SelectOption>(); 
   set<String> setUniqueRegion = new set<String>();
   for(OppCountryRegionMapping__c setting :         OppCountryRegionMapping__c.getAll().values()){
   if(!setUniqueRegion.contains(setting.Region__c)){
       options.add(new SelectOption(setting.Region__c, setting.Region__c));
       setUniqueRegion.add(setting.Region__c);       
   }
  }    

    return options;
  }

   public void fetchCountry(){
       lstCountriesSelectList = new List<SelectOption>();
       system.debug('========this.region======='+this.region);
       for(OppCountryRegionMapping__c setting : OppCountryRegionMapping__c.getAll().values()) {

      if (setting.Region__c== this.region) {
            lstCountriesSelectList.add(new SelectOption(setting.name,setting.Country__c));
        }
      }
   }
 }
11
  • Hi Ratan, I tried this code....still its not working....The country picklist is not displaying any value. The fetchCountry method's debug statement is also not displaying in the logs.Its not fetching country data. Jan 18, 2016 at 8:35
  • Hi Ratan, I have updated the code in my question. Now whichever region i select i am getting the corresponding countries.....But if i select another region the selected region's countries are appending to previous region's countries in the picklist.How to solve this? Jan 18, 2016 at 9:12
  • No i dont want that.... Jan 18, 2016 at 9:17
  • When i select new region....previous region's country list should not display in the country picklist... Jan 18, 2016 at 9:19
  • I tried your code....what's happening is the selected region's all corresponding countries are not displaying....only one country is displaying in the picklist Jan 18, 2016 at 9:20
1

You would need to send an ajax request in order to get the updated countries values. You can use actionsupport for this purpose, try the below code:

<apex:page Controller="testing_custom">
   <apex:form id="frmId">
     <apex:selectList label="Region" value="{!region}" size="1" multiselect="false">
      <apex:selectOptions value="{!regionsSelectList}"/>
      <apex:actionSupport event="onselect" action="{!getCountriesSelectList} rerender="frmId" status="SearchStatus /> 
     </apex:selectList>
 <apex:selectList  label="Country" value="{!country}" size="1" multiselect="false">
      <apex:selectOptions value="{!CountriesSelectList}}"/>
     </apex:selectList>
   </apex:form>
  </apex:page>

In your controller there is a mismatch between the things you are saving in the selectOptions and compairing:

you need to change this if condition :

if (setting.Region__c== this.region)

to

if (setting.name == region)

as you the value which is passed is "setting.name":

options.add(new SelectOption(setting.name, setting.Region__c));

updated code:

public List<SelectOption>  getCountriesSelectList() {

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

     for(OppCountryRegionMapping__c setting : OppCountryRegionMapping__c.getAll().values()) {

      if (setting.name == region) {
        options.add(new SelectOption(setting.name,setting.Country__c));

        }

      }
       system.debug('*****selected value inside'+options);
       return options;
      }
7
  • Hi Nitin, Thanks for the reply,Still Country picklist is not rendering any values. Jan 18, 2016 at 7:02
  • Can you try the "onchange" event like this :<apex:actionSupport event="onChange" action="{!getCountriesSelectList} rerender="frmId" status="SearchStatus /> </apex:selectList>
    – Nitin
    Jan 18, 2016 at 7:09
  • I tried,Same problem no change. Jan 18, 2016 at 7:16
  • there was an issue in the controller. Please find the updated code
    – Nitin
    Jan 18, 2016 at 7:28
  • Nitin,i updated the code as u have given....still the country picklist is not showing any values. Jan 18, 2016 at 8:31

You must log in to answer this question.

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