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
MSVRadMSVRad 

Dependent Picklists in VF?

Is it possible to create a dependant picklist in visualforce?  What I mean by that is I have a picklist A and that picklist has options 1,2,3,4.

 

If a user selects option 1 then another picklist will appear call it Picklist B and it has options:5,6,7,8. 

 

Each option in Picklist A will have its own Dependent picklist.

 

I know that chapter 12 of the force.com developer guide pretty much walks through something like this but what I want is slightly different in that the field is not sitting on the page at all times.  The fields I want to use already have a set picklist I just want the dependent picklist field to appear when an option is chosen from the "master" picklist.

 

Any suggestions would be great! Thanks!

Message Edited by MSVRad on 05-28-2009 02:04 PM
Best Answer chosen by Admin (Salesforce Developers) 
kevinwukevinwu

Hi,

 

How would I use your example if my picklist values are already set in a field for my object? Is there some way I can use "describe" functionality to grab the correct picklist values depending on the controlling picklist?

 

 

Thanks!

All Answers

sfdcfoxsfdcfox

This requires a combination of Visualforce controller code and <apex:actionSupport>. Example:

 

<apex: page controller="theController">

  <apex: form id="theForm">

    <apex: selectList value="{!value1}">

    <apex: actionSupport event="onchange" rerender="theForm" />

    <apex: selectOptions value="{!value1items}" />

    </apex: selectList>

    <apex: selectList value="{!value2}" rendered="{!displayfield2}">

    <apex: selectOptions value="{!value2items}" />

    </apex: selectList>

  </apex: form>

</apex: page>

 

 public class theController  {

    private string value1, value2;

    public theController() {

      value1=value2='';

   }

   public void setvalue1(string s) {

     value1 = s;

   }

   public string getvalue1() {

     return value1;

   }

   public void setvalue2(string s) {

     value2 = s;

   }

   public string getvalue2() {

     return s;

   }

   public list<selectoption> getvalue1items() {

      list<selectoption> options = new list<selectoption>();

      options.add( new selectoption ('','choose 1'));

      options.add( new selectoption ('1','value 1'));

      options.add( new selectoption ('2','value 2'));

      options.add( new selectoption ('3','value 3'));

      options.add( new selectoption ('4','value 4'));

      return options;

   }

   public boolean getdisplayfield2 {

     if(value1=='1') {

       return true;

     }

     return false;

   }

   public list<selectoption> value2items() {

     list<selectoption> options = new list<selectoption>();

     options.add( new selectoption ('','choose 1'));

     options.add( new selectoption ('5','value 5'));

     options.add( new selectoption ('6','value 6'));

     options.add( new selectoption ('7','value 7'));

     options.add( new selectoption ('8','value 8'));

     return options;

   }

}

 

 

Message Edited by sfdcfox on 05-28-2009 03:39 PM
MSVRadMSVRad

This example is very helpful, I do have one question about the actionSupport component. If a user enters information into other inputfields on the page first then the page gets rerendered because the user selects an option from the picklist after the fact will the information that they had previously entered be wiped out with the rerender or will the information remain entact?

 

I ask because the workflow for the users will change if the refresh performed leaves the page with blank fields where the user had entered information already.

 

Thanks for the help!

kevinwukevinwu

Hi,

 

How would I use your example if my picklist values are already set in a field for my object? Is there some way I can use "describe" functionality to grab the correct picklist values depending on the controlling picklist?

 

 

Thanks!

This was selected as the best answer
kvinkvin

Hey ,

 

Did you get a solution for the above ?. Please share ua knowledge, greatly appreciate if someone come up with a sample code and explanation.

 

thanks