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
suresh bangaloresuresh bangalore 

ABOUT VF PAGE

Hi
How to create a dependent picklist in visualforce using  cusom object....assist me I m new salesforce
 
Sunil MadanaSunil Madana
Apex-Code
public class Class_906F0000000DADAIA4 {
    public string conti{get; set;}
    public string contr{get; set;}
    public string stat{get; set;}
    public string regi{get; set;}
    public string city{get; set;}
    public Class_906F0000000DADAIA4(ApexPages.StandardController controller) { }
    public list<selectoption> getcontinentobj(){
        list<selectoption> options= new list<selectoption>();
        list<Continent__c> conti= [select name from Continent__c order by name];
        options.add(new selectoption('—-Select Anyone—-', '—-Select Anyone—-'));
        for(Continent__c c:conti){
            options.add(new selectoption(c.name,c.name));
		}
        return options;
	}
                                          
    public list<selectoption> getcontryobj(){        
        list<selectoption> options= new list<selectoption>();
        list<Country__c> contr= [select name  from Country__c where Continent__r.name=:conti];
        options.add(new selectoption('—-Select Continent—-', '—-Select Continent—-'));
        for( Country__c c:contr){
            options.add(new selectoption(c.name,c.name));
        }
        return options;
    }
    
    public list<selectoption> getstateobj(){        
        list<selectoption> options= new list<selectoption>();
        list<State_Provice__c> stat= [select name from State_Provice__c where Country__r.name=:contr];
        options.add(new selectoption('—-Select Country—-', '—-Select Country—-'));
        for(State_Provice__c s:stat){
            options.add(new selectoption(s.name,s.name));            
        }
        return options;
    }
    
    public list<selectoption> getregionobj() {
        list<selectoption> options= new list<selectoption>();
        list<County_Region__c> regi= [select name from County_Region__c  where State_Provice__r.name=:stat];
        options.add(new selectoption('—-Select State—-', '—-Select State—-'));                                              
        for(County_Region__c r:regi){ options.add(new selectoption(r.name,r.name)); }
        return options;
    }
    
    public list<selectoption> getcityobj(){                                              
        list<selectoption> options= new list<selectoption>();                                              
        list<City__c> city= [select name from City__c where County_Region__r.name=:regi];                                              
        options.add(new selectoption('—-Select Region—-', '—-Select Region—-'));
        for(City__c c:city){ options.add(new selectoption(c.name,c.name)); }
        return options;
    }
}

Visualforce-Page
<apex:page standardController=”Continent__c” extensions=”MultilevelDependentPicklist”>
    <apex:sectionHeader title=”Find your location” subtitle=”{!Continent__c.name}”/>
        <apex:form >
            <apex:pageBlock title=”Location” mode=”edit”>
                <apex:pageBlockSection columns=”1″>
                    <apex:pageblockSectionItem ><apex:outputLabel value=”Continent Name”/>
                        <apex:outputPanel styleClass=”requiredInput” layout=”block”>
                        <apex:outputPanel styleClass=”requiredBlock” layout=”block”/>
                            <apex:selectList size=”1″ value=”{!conti}”>
                                <apex:selectoptions value=”{!continentobj}”/>
                                <apex:actionSupport event=”onchange” reRender=”country”/>
                            </apex:selectList>
                        </apex:outputPanel>            
                    </apex:pageblockSectionItem>
                    <apex:pageblockSectionItem >
                        <apex:outputLabel value=”Country Name”/>
                        <apex:selectList size=”1″ value=”{!contr}” id=”country”>
                            <apex:selectoptions value=”{!contryobj}”/>
                            <apex:actionSupport event=”onchange” reRender=”states”/>
                        </apex:selectList>
                    </apex:pageblockSectionItem>
                    <apex:pageblockSectionItem >
                        <apex:outputLabel value=”State / Provice”/>
                        <apex:selectList size=”1″ value=”{!stat}” id=”states”>
                            <apex:selectoptions value=”{!stateobj}”/>
                            <apex:actionSupport event=”onchange” reRender=”regions”/>
                        </apex:selectList>
                    </apex:pageblockSectionItem>
                    <apex:pageblockSectionItem >
                        <apex:outputLabel value=”County / Region”/>
                        <apex:selectList size=”1″ value=”{!regi}” id=”regions”>
                            <apex:selectoptions value=”{!regionobj}”/>
                            <apex:actionSupport event=”onchange” reRender=”cities”/>
                        </apex:selectList>
                    </apex:pageblockSectionItem>
                    <apex:pageblockSectionItem >
                        <apex:outputLabel value=”City”/>
                        <apex:selectList size=”1″ value=”{!city}” id=”cities”>
                            <apex:selectoptions value=”{!cityobj}”/>
                            <apex:actionSupport event=”onchange” reRender=”village”/>
                        </apex:selectList>
                    </apex:pageblockSectionItem>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </apex:form>
</apex:page>

Hope it helps. Thanks.
Sunil MadanaSunil Madana
If the above suggestion(s) worked, let us know by marking the answer as "Best Answer" right under the comment which will help the rest of the community should they have a similar issue in the future. Thanks.
JyothsnaJyothsna (Salesforce Developers) 
Hi Suresh,

Please check the below sample code.
Visualforce Page:
Visualforce page:

<apex:page controller="sample">
    
    <apex:form >
    
    <apex:pageBlock>
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem>
                <apex:outputLabel value="State"/>
            </apex:pageblockSectionItem>        
            <apex:pageblockSectionItem>                
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
                </apex:selectList>                
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem>
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>            
            <apex:pageblockSectionItem>
                <apex:selectList size="1" value="{!city}" id="a">
                    <apex:selectOptions value="{!cities}"/>
                </apex:selectList>
            </apex:pageblockSectionItem>            
        </apex:pageBlockSection>        
    </apex:pageBlock>

    </apex:form>

</apex:page>

Controller Class:
public class sample
{
    public String state {get;set;}
    public String city {get;set;}

    public List<SelectOption> getStates()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--- None ---'));        
        options.add(new SelectOption('TN','Tamil Nadu'));
        options.add(new SelectOption('KL','Kerala'));
        return options;
    } 
    
    public List<SelectOption> getCities()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(state == 'TN')
        {       
            options.add(new SelectOption('CHE','Chennai'));
            options.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state == 'KL')
        {       
            options.add(new SelectOption('COA','Coachin'));
            options.add(new SelectOption('MVL','Mavelikara'));
        }
        else
        {
            options.add(new SelectOption('None','--- None ---'));
        }      
        return options;
    }       
}

Hope this helps you!
Best Regards,
Jyothsna