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
Srinivas AnnamSrinivas Annam 

how can we do field dependency through apex coding?

Best Answer chosen by Srinivas Annam
JyothsnaJyothsna (Salesforce Developers) 
Hi Srinivas,

Please check the below sample code.

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

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi Srinivas,

Please check the below sample code.

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 Code:
 
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
This was selected as the best answer
DeepthiDeepthi (Salesforce Developers) 
Hi Srinivas,

Please check out the below links that answers your requirement:
https://developer.salesforce.com/forums/?id=906F0000000Ar9DIAS 

https://developer.salesforce.com/forums/?id=906F0000000Aye5IAC 

Hope this helps you!
Best Regards,
Deepthi
JyothsnaJyothsna (Salesforce Developers) 
Hi Srinivas,

I tried this code and it is working properly. Can you please share the exact error message that you are facing?

Regards,
Jyothsna
Srinivas AnnamSrinivas Annam
sorry ,its working...thank you.