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 value in a variable from selectOptions

Hello team, 
How could I catch the value {!items} selected and capture it in a variable? I need a simple line of code. 

This is my VF page
<apex:pageBlock tabStyle="Account" title="Merge Accounts">
              <apex:pageBlockSection title="Choose Accounts to Merge" collapsible="false">
                <apex:selectList label="Account 1" value="{!accounts}" size="1">
                    <apex:selectOptions value="{!items}"/>
                </apex:selectList>
                  <!--//[Account to merge]-->
                <apex:selectList label="Account 2" value="{!accounts}" size="1">
                    <apex:selectOptions value="{!items}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageBlock>

Here is the methods in controller
 
public String[] accounts = new String[]{};
            
        public PageReference test() {
            return null;
        }
    	//Here the accounts are invoke
        public List<SelectOption> getItems() 
		{
			List<Account> lstAcc = [select id,name from account limit 10];
			List<SelectOption> options = new List<SelectOption>();

			for(Account acc : lstAcc)
			{
				options.add( new SelectOption( acc.id,acc.Name ));
			}
            return options;
        }
        //Here we get the accounts in getiing them
        public String[] getAccounts() {
            return accounts;
        }
        //Here we set the accounts in setting them
        public void setAccounts(String[] accounts) {
            this.accounts = accounts;
        }

I need to catch the value from selectList in a variable to make validations in a new method.
Thanks in advance.
sfdcsushilsfdcsushil
Hi,

Value attribute is used for binding the selected value as per my understading. You can define it as Id or String, in your code you have defined it as list. Also you are binding two picklists to same value, i dont think that would work.