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
john dsouzajohn dsouza 

fetch custom setting values into picklist

Hello

I have created a new custom setting say Countries (api name : Countries__c) and has field CountryCode__c
this means
 Australia AU
 India IN
 Kenya  KN

I have the visualforce page as follows.
<apex:selectList value="{!fanCountry_Region}" size="1">
   <apex:selectOption itemValue="None" itemLabel="--Select--"></apex:selectOption> <
     apex:selectOption itemValue="United Kingdom" itemLabel="United Kingdom">
  </apex:selectOption> <apex:selectOption itemValue="India" itemLabel="Germany">
</apex:selectOption>
</apex:selectList>

I need to remove the above values and need to fetch the values from the custom setting .

So please let me know what changes should I make to visualforce page and how to fetch values in controller for picklist.

assume , I select Australia from picklist in visualforc page then it gets saved to sobject.

any help will be grealty appreciated

Thanks
John D
Shruti SShruti S

You can display the values in the Visualforce page with the help of get set properties. Query the records from your Custom Settings (Countries__c) and assign them to the get set variables. Once you get the values from your Custom Settings, you can iterate them and make SelectOption instances which can be used to generate the SelectOption tags in Visualforce page.

Here is the Apex code and the Visualforce markup of what exactly you should be writing.

Apex Controller

public class CountryCodeGeneratorController {
    public List<SelectOption> countryNames { get; set; }
    
    public CountryCodeGeneratorController() {
        getCountryNames();
    }
    
    public void getCountryNames() {
        countryNames = new List<SelectOption>();
        
        List<Countries__c> allCountries = new List<Countries__c>();
        
        allCountries = [
            SELECT  Name
                    ,CountryCode__c
            FROM    Countries__c
        ];
        
        for( Countries__c country : allCountries ) {
            countryNames.add( new SelectOption( country.Id, country.Name ) );
        }
    }
}

Visualforce
<apex:page controller="CountryCodeGenerator">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Country Name :"/>
                  
                <apex:selectList size="1">
                    <apex:selectOptions value="{!countryNames}"/>
                </apex:selectList>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
john dsouzajohn dsouza
Hi
That was gr8. I only made a small modification as follows.
countryCodeList.add( new SelectOption( country.CountryCode__c, country.Name ) ) .
In backend I am storing country code in the field .

Would you mind if I make a small extension to my post.
Actually I need to create another picklist in object design  which stores names of states.

for ex: if I select Australia from the above picklist then the dependency picklist will show all states in Australia and so on.

should I create another field in custom setting say states__C and store all states?

so in controller how to retrieve the states based on country selection and what changes to be done in visual force page.

Please let me know how to proceed on this.

Thanks
John
Shruti SShruti S
You cannot use Custom Settings as there will be many values for states. Instead the Schema should be like the below - 
User-added image

There should be two custom objects, Country__c and State__c. The Country__c object should have the default field name where you will store the name of the country and the country code field to hold the country code of the country. The other custom object State__c should have a master-detail relationship field to Country__c.

If you think this method works for you then I can write the sample code for this and give.

Using this method, we have a master data set at our disposal. The only caveat is that you will have to upload the data(Countries and States) which again is a one-time process. But once when we have this in place it can be re-used for multiple use cases. I feel this is a more scalable and maintenance friendly approach as opposed Custom Settings. Custom Settings as the name suggests are designed to hold only key data. If we were to stored both Country/State info on that it would be a lot of information and it would against the idea of "settings".
john dsouzajohn dsouza
Hi
Thanks for your functional idea. I discussed this with my chief technical architect and he is fine with this approach.

so if I were to go with your approach then what about piece of code I posted , would that hold or I need to do away with that?

could you pls send me the code along with changes in visual force page.

Thanks
JohnD
john dsouzajohn dsouza
HI
there is one more thing I want to discuss.
There is a custom object called fan__c which has a field countryregion__c , data type=text.

so when the page loads I am appending the fanID to the url so thereby fetching the data into respective fields.

so if I select Australia from vf page then its corresponding code CA gets stored in backend.

This is happening right now.

The issue is if I create two separate objects then how to relate to the primary object fan__c.
then countryregion__c may become redundant.

I hope I am clear.

Thanks
John