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

How to select city in a picklist by selecting a state in another picklist?

I have made an object called "State". There are three custom fields :

                                                                     1. State

                                                                     2. City

                                                                     3. Village

                                                                      in this object.

I have made  picklists for state, city and village . I want that whenever i select a state, city associated with this state got select. And also when i want that  whenever i select a city, Village associated with this city got select.If you have any solution please tell me.Thanks .

I have written code for that which is as follow:-

                                             For page

<apex:page controller="sampleCon">
<apex:form >
<apex:actionFunction name="Citysearch" action="{!getCities}" reRender="result" status="status1"/>
<apex:actionFunction name="Villagesearch" action="{!getVillages}" reRender="result" status="status2"/>

<apex:selectList value="{!State}" size="1" onclick="searchCity()" >
<apex:selectOptions value="{!items}" />
<apex:actionStatus startText="Searching..." stopText="Search Result" id="status1" >

<apex:selectList value="{!City}" size="1" onclick="searchVillage()">
<apex:selectOptions value="{!Cities}"/>
<apex:actionStatus starttext="searching..." stoptext="Search Complete" id="status2" >

<apex:selectList value="{!Village}" size="1">
<apex:selectOptions value="{!Villages}"/>

function searchCity(){
// alert('hiiiiiiiiiii');
function searchVillage()
// alert('hello');
For controller
public class sampleCon {
        String id;
        public StateCity__c State{get;set;}
        public StateCity__c City{get;set;}
        public StateCity__c Village{get;set;}
        public List<SelectOption> getItems() {
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Rajasthan'));
                    options.add(new SelectOption('id','MP'));
                    return options;
        public List<SelectOption> getCities() {
                    id = ApexPages.currentPage().getParameters().get('id');
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Ajmer'));
                    options.add(new SelectOption('id','Bhopal'));
                    return options;
        public List<SelectOption> getVillages() {
                    id = ApexPages.currentPage().getParameters().get('id');
                    List<SelectOption> options = new List<SelectOption>();
                    options.add(new SelectOption('id','Kishangarh'));
                    options.add(new SelectOption('id','Nimach'));
                    return options;

There is anyone who can tell me.Please reply.


Hi there,


Have you already tried doing this using field dependecies? They are really easy to configure and

you can have up to300 values in the controlling field. Unless there is another reason you need to write code for it


But if you want to try field dependencies and haven't done it before, try this:

Go to Setup|AppSetup|Customize|YourObject|Fields Click on the Field Dependencies Button. 

Then click on New

Then choose the controlling and dependent fields from the drop downs

Then on the next screen you select your include and exclude values, now all you have to do is place the fields you want in the right place


Hello Balkishan, I am trying to implement the similar solution on our org for easier and error free address input and found your code is very promising. Wondering if you were able to achieve this completely with out issues after you posted this on the forum. If yes, would you please let me know how the whole thing worked out for you.


Thank you in advance