2

I am a newbie,so i dont know how to work around with custom settings.In the requirement there is a custom setting object called OppCountryRegionMapping__c.In this object there are 2 fields,region__c and country__c.Below is the screenshots of custom settings.

enter image description here

When i click on manage there are records,which have records.

enter image description here

When i select particular record,it will have the values for Name,region and country.

Like

 Name-->AustraliaAsia
 Region-->Asia
 Country-->Australia

I have to develop a page where region and country should be picklist.The region should show all distinct regions and when i select particular region from the picklist the corresponding countries should be displayed in the country picklist.

I have tried with the following visualforce and apex code.

Visualforce

      <apex:page Controller="testing_custom">
       <apex:form >
         <apex:selectList label="Region" value="{!region}" size="1" multiselect="false">
          <apex:selectOptions value="{!regionsSelectList}"/>
         </apex:selectList>
     <apex:selectList label="Country" value="{!country}" size="1" multiselect="false">
          <apex:selectOptions value="{!CountriesSelectList}}"/>
         </apex:selectList>
       </apex:form>
      </apex:page>

Apex

   public class testing_custom
   {
       public String region{get;set; }
       public String country{get;set;}

    //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.name, 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.name,setting.Country__c));

        }

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

This is getting duplicate values of region also,

i tried placing the values in the set.But it will not work.How have to proceed with this.Help me.

Thanks

0

If you removed one line i.e. options.add(new SelectOption(setting.name, setting.Region__c)); and still getting duplicates values.

So Issue with your custom setting record..

Not sure with your business logic. But As you asked for solving duplicate region issue. Then

You can resolve your duplicate region issue with salesforce collection set unique feature.

Do like below

 public class testing_custom
   {
       public String selectedValue{get;set; }

       public List<SelectOption> getMyOptions(){ 
       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.name, setting.Region__c));
            setUniqueRegion.add(setting.Region__c);       
        }
      }    

      return options;
  }
}
3
  • Hi Ratan, I tried the code that you have written,its working fine...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.I have edited the apex class and visualforce in my question.Please check it. – Learner_sfdc Jan 18 '16 at 5:25
  • @Learner_sfdc looks like you are asking another question here.. I suggest first accept this ans and close this question and ask in another question and give me here your new question link I will try to solve your issue – Ratan Paul Jan 18 '16 at 6:28
  • Hi Ratan, The link for my question. salesforce.stackexchange.com/questions/105711/… – Learner_sfdc Jan 18 '16 at 6:35
0

I saw that you put the same code in loop body in twice.

for(OppCountryRegionMapping__c setting : OppCountryRegionMapping__c.getAll().values()){
    options.add(new SelectOption(setting.name, setting.Region__c));
    options.add(new SelectOption(setting.name, setting.Region__c));
}   
1
  • Hi Allen, Thanks for the repy...i removed the extra line..even then duplicate values are coming.. As i have added Region as ASIA for more than one country in the custom settings record.How can i make only ASIA or any other region to appear once in the picklist. – Learner_sfdc Jan 17 '16 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.