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
Rodolfo CalvoRodolfo Calvo 

Get Boolean value from selectList - SelectOption

Hello team, 
I need to get the boolean from a selectList. 
For example I have the following code.
 
<apex:selectList id="chooseRule" value="{!}" size="1">
                                <apex:selectOption itemEscaped="false" itemValue="bContains" itemLabel="Contains"/>
                                <apex:selectOption itemEscaped="false" itemValue="bStart" itemLabel="Start with"/>
                                <apex:selectOption itemEscaped="false" itemValue="bNotEqual" itemLabel="Not Equal to"/>
                            </apex:selectList>

How can I show in my code behind, something like: 
   
If(bContains.isSelected){ do something}

Something like that?. 
Thanks!
Best Answer chosen by Rodolfo Calvo
srlawr uksrlawr uk
you don't have to use object fields either... if you define a variable in your controller, such as 
 
public string fromSelect { get; set; }

then in the value of your select list attribute "value" put {!fromSelect} when you submit the form it will bind the value from the select option to that string in your controller, allowing you to then use it as you have said, simply in an IF statement.

Note also there is a ".equals" method on the string method so for logical equality, use something like
 
if('bContain'.equals(fromSelect)) {
}

 

All Answers

RohRoh
Hello Rodolfo,
yes, you can do this .
for example : 

<apex:selectList id="chooseRule" value="{!object.field__c}" size="1">
                                <apex:selectOption itemEscaped="false" itemValue="bContains" itemLabel="Contains"/>
                                <apex:selectOption itemEscaped="false" itemValue="bStart" itemLabel="Start with"/>
                                <apex:selectOption itemEscaped="false" itemValue="bNotEqual" itemLabel="Not Equal to"/>
                            </apex:selectList>
In Controller :
If (object.field__c == 'bContains')
{
  //do something
}

PLEASE SELECT THIS AS THE BEST ANSWER, IF YOU LIKE IT.

Thanks,
Rohit Alladi
srlawr uksrlawr uk
you don't have to use object fields either... if you define a variable in your controller, such as 
 
public string fromSelect { get; set; }

then in the value of your select list attribute "value" put {!fromSelect} when you submit the form it will bind the value from the select option to that string in your controller, allowing you to then use it as you have said, simply in an IF statement.

Note also there is a ".equals" method on the string method so for logical equality, use something like
 
if('bContain'.equals(fromSelect)) {
}

 
This was selected as the best answer