+ Start a Discussion

Generate List based on a Picklist Value

I'm new to Visualforce and looking to create a page that maps out this basic workflow:


1) User selects a value from a picklist (not tied to any specific object).

2) A list of records for a specific entity is generated based on the value selected in the picklist.


How can I create a basic field and then use that selected value to plug-in to another query / list?




Use a page controller and have a SelectList that is filled with Select Options that you control.  Use a command button with a page action that refreshes the page after filling a controller variable with the query results.  You can get fancy with ajax as well (that's this whole action status bit)


<apex:page standardController="Custom_Object__c" extensions="QueryController" >

<apex:form id="frmSelectSigItems">

<apex:pageBlock title="Signature Report SO Selection" >

<apex:pageBlockSection title="Query Form" collapsible="false" columns="1">

<apex:outputPanel id="selectedSOs">

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

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



<apex:commandButton value="Query Stuff" action="{!doQuery}"

rerender="outputList" status="status"/>


<apex:pageBlockSection title="Query Results" collapsible="false" columns="1">

<apex:outputPanel id="outputList">

<apex:actionstatus id="status" startText="Querying...">

<apex:facet name="stop">

<apex:outputPanel >

<p>Query Results</p>

<apex:dataList value="{!myObjects}" var="obj">

{!obj.Name} | {!obj.id} </apex:dataList>







Controller code (named QueryController as an example, change it to whatever):


private String[] selectedItems = new String[]{};
private List<Custom_Object__c> queryResults = new List<Custom_Object__c>();
List<SelectOption> options;


public List<SelectOption> getItems() {
options = new List<SelectOption>();
String[] allPicklistItems = new String[]{'one', 'two', 'three'};
        for (String eachItem:allPicklistItems){
options.add(new SelectOption(eachItem, eachItem);
return options;
public void setSelectedItems(String[] selection){
selectedItems = selection;

public String[] getSelectedItems(){
return selectedItems;

and finally, the doQuery:


public void doQuery(){

    queryResults = [select id, name from Custom_Object__c where name = :selectedItems[0]];



I haven't tested the doQuery but the rest of the stuff works for me in a slightly different implementation.  you should get the gist of it


There's some good docs on this in the Visualforce Developer's guide here:





gtuerk - much appreciated, thank you for the detailed response!