[SalesForce] How to display a picklist based on the value chose on another picklist

I need to display two picklist in VF page. I have created a picklist using selectoption (Say PickList1) which has values of '1,2,3,4,5'. Now on selecting a value from this picklist i need to display the second picklist. For eg , if I select 1 in picklist1 , second picklist needs to display 2,3,4,5.

Best Answer

From the Visualforce Developer Guide:

Adding Dependent Fields to a Page

Dependent fields provide a way to filter the field values displayed on a Visualforce page. Dependent fields consist of two parts: a controlling field that determines the filtering, and a dependent field that has its values filtered. Dependent fields can dynamically filter values in fields such as picklists, multi-select picklists, radio buttons, and checkboxes. Dependent picklists can only be displayed on Visualforce pages with Salesforce API version 19.0 or higher. For more information, see Dependent Picklists in the Salesforce online help.

For this example, we’ll be adding a dependent picklist, Subcategories, to a Visualforce page. First, create this custom picklist:

  1. From the object management settings for accounts, go to the fields area, and then click New.
  2. Choose Picklist, and then click Next.
  3. Enter Subcategories for the Field Label.
  4. Enter the following terms for the list of values:

    • Apple Farms
    • Cable
    • Corn Fields
    • Internet
    • Radio
    • Television
    • Winery
  5. Click Next twice, then click Save.

To define the field dependencies for Subcategories:

  1. From the object management settings for accounts, go to the fields area.
  2. Click Field Dependencies.
  3. Click New.
  4. Choose Industry as a controlling field, and Subcategories as a dependent field.
  5. Click Continue.
  6. Each value in the controlling field (from Industry) is listed in the top row and each value in the dependent field (from Subcategory) is displayed in the column below it. Set your field dependencies to match this image:

    The Field Dependency Matrix for Subcategories

    The field dependency matrix for Subcategories

    You can disregard any other Industry types that aren’t shown above.

Now, create a Visualforce page called dependentPicklists that looks like this:

<apex:page standardController="Account">
    <apex:form >
        <apex:pageBlock mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Dependent Picklists" columns="2">
            <apex:inputField value="{!account.industry}"/>
            <apex:inputField value="{!account.subcategories__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

When you select Agriculture from the Industry picklist, the Subcategories picklist contains Apple Farms, Corn Fields, and Winery. If you select Communication, your Subcategories picklist contains all the Communication types defined earlier.

Related Topic