+ Start a Discussion

How do I access the selected items from a selectList in my custom controller?

The question says it all, really. I have a VF page where I can get the information I need. All of the inputText and inputTextarea fields are directly accessible. I cannot, however, figure out how to get at the selected values in my two selectLists. One selectList is single-select, and the other one is multi-select.


I saw a post elsewhere on these boards that said that I needed to add javascript to the onclick event to get at the selected value. This wouldn't work for multi-select anyway, and I cannot believe that there isn't a straightforward way to get at this information.


So what is it?





Best Answer chosen by Admin (Salesforce Developers) 
Found the problem -- I removed the default values and the selectLists started working properly.

All Answers


selectList has a value attribute.  When your form is submitted, the setter gets called on the expression that you've supplied to your value attribute.  You do not need javascript to get these values, and there is an example in the component reference that shows you how to use selectList.


Bind your value attribute to a String for the single-select, and a collection of strings for the multi.


Well, I tried that. Here's the VF:


        <apex:pageBlockSectionItem id="fileAccessSection">
          <apex:outputLabel for="theAccess" value="File Access" />
          <apex:selectList id="theAccess" value="{!theAccess}" size="1" required="true">
            <apex:selectOption itemValue="private" itemLabel="private" />
            <apex:selectOption itemValue="public" itemLabel="public" />
        <apex:pageBlockSectionItem id="fileFolderSection">
          <apex:outputLabel for="theFolder" value="Folder" />
          <apex:selectList id="theFolder" value="{!theFolder}" size="1">
            <apex:selectOptions value="{!folderList}"/>


Obviously, one selectList is manual and the other is taken from an SF object. Here's the code from the controller:



public String theAccess { get; set; } public String theFolder { get; set; } List<SelectOption> folderList = new List<SelectOption>(); // Get a list of all folders for the data entry page. public List<SelectOption> getFolderList() { List<My_Folder__c> folders = [SELECT Name FROM My_Folder__c ORDER BY Name ASC]; if (folders != null) { for (Cloud_Folder__c folder : folders) { folderList.add(new SelectOption(folder.Name, folder.Name)); } } return folderList; }


I'm not sure if it makes a difference, but I default theAccess and theFolder in the constructor event of my controller.


No matter what I select, the default value is shown in theAccess and theFolder when I submit the form. Is it that I have defaults for these values? Or am I making a mistake in my code?













Found the problem -- I removed the default values and the selectLists started working properly.
This was selected as the best answer