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
Naresh Kumar Mohan ThirukkovaluruNaresh Kumar Mohan Thirukkovaluru 

how to get connected to sbjects thru controller class for getting states & its capitals

public with sharing class sample {

    public List<selectoption> getCities() {
   
  
        List<selectoption> op1= new List<selectoption> ();
        if(state == 'AP')
        {
        List<dependncs__c> rt = [select city__c from dependncs__c];
         string nb;
         for(dependncs__c c : rt)
         nb=c.city__c;
        op1.add(new selectoption(nb,nb));
        }
       else if(state == 'TN')
       {
        //op1.add(new selectoption('CHEN','CHENNAI'));
       }
       else
        {
            //op1.add(new SelectOption('None','--- None ---'));
        }     

        return op1;
    }


    public String city { get; set; }

    public  List<selectoption> getStates() {
       List<dependncs__c> rt = [select state__c from dependncs__c];
       List<selectoption> op= new  List<selectoption> ();
       string na;
       for(dependncs__c b:rt){
       na=b.state__c;
       op.add(new selectoption(na,na));
       }
       
        
        return op;
    }


    public String state { get; set; }
}





VF
<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>




AshwaniAshwani
Create a related  custom object instance in apex class:

public with sharing class sample {

    public Contact myContact {get;set;}
    
public Sample()
{
  myContact = new Contact();
}
    public List<selectoption> getCities() {
   
  
        List<selectoption> op1= new List<selectoption> ();
        if(state == 'AP')
        {
        List<dependncs__c> rt = [select city__c from dependncs__c];
         string nb;
         for(dependncs__c c : rt)
         nb=c.city__c;
        op1.add(new selectoption(nb,nb));
        }
       else if(state == 'TN')
       {
        //op1.add(new selectoption('CHEN','CHENNAI'));
       }
       else
        {
            //op1.add(new SelectOption('None','--- None ---'));
        }     

        return op1;
    }


    public String city { get; set; }

    public  List<selectoption> getStates() {
       List<dependncs__c> rt = [select state__c from dependncs__c];
       List<selectoption> op= new  List<selectoption> ();
       string na;
       for(dependncs__c b:rt){
       na=b.state__c;
       op.add(new selectoption(na,na));
       }
       
        
        return op;
    }


    public String state { get; set; }
}

Reference in visualforce as:


<apex:page controller="sample">
  <apex:form >
    <apex:inputfield value="{!myContact.FirstName}"/>

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


Naresh Kumar Mohan ThirukkovaluruNaresh Kumar Mohan Thirukkovaluru
its not working 

i want to get states and their capitals as i inserted records TN=chen, AP=hyd

so i got states list in my page their corresponding capitals i m unable to get thru controller clas