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
krishna casukhela 7krishna casukhela 7 

How to fetch values into a dependent picklist in apex

I have a country picklist in visualforce page and the country names are stored in a custom setting(countries__c), under this I created a field called countrycode__c.
If I select India then in object 'IN'  gets stored.

so in visual force page I am fetching country names
public class CountryCodeGeneratorController {

    public List<SelectOption> countryNames { get; set; }


    public CountryCodeGeneratorController() {


    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 ) );


Now in visual force page I create another picklsit say states and what I want to achieve is for every country selected I want to fetch the corresponding states. I do not think its required to create a picklist in object design. also where to store the state names. Please let me know how to achieve this.

NagendraNagendra (Salesforce Developers) 
Hi Krishna,

May I suggest you please check with below help document which might help you accelerate further with above requirement. Hope this helps.