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

<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}"/>
              <!--//[Account to merge]-->
            <apex:selectList label="Account 2" value="{!accounts}" size="1">
                <apex:selectOptions value="{!items}"/>

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.


The value attribute of the apex:selectList tag holds a selected value from the list. You should define an apex variable for that:

public string selectedValue { get; set; }

<apex:selectList label="Account 1" value="{!selectedValue}" size="1">

For more info just read the manual: apex:selectList

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.