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
Erik RodgersErik Rodgers 

how to access selectoption "value" selected

Hello. I am new to the Force.com platform and I am trying to understand how to access the "value" parameter value of the SelectedOption for the <apex:selectList> or <apex:selectOptions> components when the page is submitted. I want to access this selected value from my doSave method. I have searched the forum and see where a developer can bind a variable to the "label" for the Selected Option, but not the "value". For example, if I add the following SelectedOptions:

options.add(new SelectOption('789521','Some Category'));
options.add(new SelectOption('986521','Some Other Category'));
options.add(new SelectOption('143254','Yet Another Category'));

Then in my doSave method, I need to be able to access the value of the selected option (e.g., 986521) - not the label (Some Other Category) - for processing. The label was only useful for displaying to the user, not for processing (as the value I placed in the SelectOption is actually a key in an external system with which we integrate). Since we pass this value as part of the constructor for SelectOption, I would only assume there is some way to access it when submitted server-side. I would greatly appreciate any help the community can provide. Thanks!
 
Best Answer chosen by Erik Rodgers
Erik RodgersErik Rodgers
It turns out I was asking a ridiculous question all along. The selected value variable bound to the <apex:selectList> component was infact the value and not the label. I think I was confused while reviewing a use-case example and read that example to imply that the label and not the value was being set in the variable bound to the <apex:selectList> component. That is not the case. If I wanted the label and the value, I would probably need to use some type of mirrored map variable.

All Answers

Sagar PareekSagar Pareek
http://sfdcsrini.blogspot.com/2014/07/how-to-use-apexselectoptions-and.html
Erik RodgersErik Rodgers
Thank you for your response Sagar, but the link you referenced does not say anywhere how to retrieve the selected value. It instead, like all other examples I've seen, shows how to retrieve the selected label from the select list. The label is of no use to me.
Dushyant SonwarDushyant Sonwar
Hi Erik,
Use this
options[indexnumber].getvalue()
Erik RodgersErik Rodgers
Thank you Dushyant, but I still do not believe this answers my question because I have no idea how you would know the "indexnumber" of the selection made by the user in the conext of the method. For example, refering to the example at:

http://www.salesforce.com/docs/developer/pages/Content/apex_pages_selectoption.htm

How would you know the value (not the label) of the selection made from the countries selectlist from the "test" method? Again, not the label, but rather the value (i.e., CANADA, not Canada)? 
Dushyant SonwarDushyant Sonwar
Hey Eric,
You can try out this function may be this could help you out
public string accessoption(string seloption){
        for(integer i=0;i<lstoption.size();i++){
            if(seloption == lstoption[i].getlabel()){
                return lstoption[i].getvalue();
            }
        }
        return null;
    }
 
Erik RodgersErik Rodgers
Hey Dushyant, I was trying to avoid looping through the original list because I figured there would be a more sensible approach built-in. I can certainly do this, but it just seems cumbersome. I may create my own utility class to handle this with a mirrored map. It just seems hard to believe this functionality isn't built-in. It really makes me question the power and flexibility of the platform.
Dushyant SonwarDushyant Sonwar
Hi Erik,
I don't think salesforce has built-in functionality for that.Your  Mirrored Map Idea would be the best approach for your problem as you already said.
 
Erik RodgersErik Rodgers
It turns out I was asking a ridiculous question all along. The selected value variable bound to the <apex:selectList> component was infact the value and not the label. I think I was confused while reviewing a use-case example and read that example to imply that the label and not the value was being set in the variable bound to the <apex:selectList> component. That is not the case. If I wanted the label and the value, I would probably need to use some type of mirrored map variable.
This was selected as the best answer