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
RadDude89RadDude89 

Automatically update dependent picklist on controlling picklist selection

Hi,

Is it possible to update a dependent picklist based on the selection of the controlling picklist?
For example - we have a list of countries on a controlling picklist and a dependent picklist of the abbreviated versions of the countries.
So if a user selected "England" then "Eng" would be available to select but the user would have to manually select it.

Can a visualforce page be changed so that if a user selected "England" then "Eng" would be automatically be selected on the dependent picklist rather the user having to select it?

Thanks in advance.

Best Answer chosen by RadDude89
DeepthiDeepthi (Salesforce Developers) 
HI RadDude,

If I'm not wrong, your requirement is like when you select a value in Controlling picklist then the associated values should be displayed in the Dependent picklist. 
If that is the case, then please check the below code snippet.
 
<apex:page controller="ControllerClass">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputLabel >State</apex:outputLabel>
            <apex:pageblockSectionItem >                
                <apex:selectList size="1" value="{!state}">
                    <apex:selectOptions value="{!states}"/>
                    <apex:actionSupport event="onchange" reRender="a"/>
                </apex:selectList>                
            </apex:pageblockSectionItem>
            <apex:outputLabel value="City"/>       
            <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>
 
public class ControllerClass {

    public String state { get; set; }
    public String city { get; set; }
    
    public List<SelectOption> getStates() {
        List<SelectOption> stOptions = new List<SelectOption>();
        stOptions.add(new SelectOption('None','--None--'));
        stOptions.add(new SelectOption('AP','Andhra Pradesh'));
        stOptions.add(new SelectOption('TN','Tamil Nadu'));
        stOptions.add(new SelectOption('KA','Karnataka'));
        return stOptions;
    }

    public List<SelectOption> getCities() {
        List<SelectOption> ciOptions = new List<SelectOption>();
        if(state=='AP'){
            ciOptions.add(new SelectOption('VSKP','Vishakapatnam'));
            ciOptions.add(new SelectOption('BZA','Vijayawada'));
            ciOptions.add(new SelectOption('TPTY','Tirupati'));
        }
        else if(state=='TN'){
            ciOptions.add(new SelectOption('CHE','Chennai'));
            ciOptions.add(new SelectOption('CBE','Coimbatore'));
        }
        else if(state=='KA'){
            ciOptions.add(new SelectOption('SBC','Bangalore'));
            ciOptions.add(new SelectOption('MAQ','Mangalore'));
        }
        else if(state=='None'){
            ciOptions.add(new SelectOption('None','--None--'));
        }
        return ciOptions;
    }

}


Hope this helps you!
Best Regards,
Deepthi