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
Dippan Patel 3Dippan Patel 3 

Unable to pass selected picklist value from Visualforce page to the controller.

Hi All, 

I want to pass the picklist value selected from the visualforce page to my controller but I am getting a null value. 
<apex:page controller="PicklistController">

  <apex:form >
      Enter Text: <apex:inputText id="txt1" value="{!textDemo}"  />

	<apex:selectList id="selectedUser" value="{!selectedUser}"  size="1"  onchange="setSelectedUser()">
       	<apex:selectOptions value="{!ActiveUsers}"></apex:selectOptions>
        
	</apex:selectList>
  </apex:form>
</apex:page>



public with sharing class PicklistController {
    public String selectedUser {get; set;}
    public String textDemo {get; set;}

    
    public List<SelectOption> getActiveUsers() {
        List<SelectOption> users = new List<SelectOption>();
        List<User> allUsers = [select name from User where isactive = true];
        system.debug(logginglevel.error, +allUsers);
        for(User u : allUsers){
        	String u1 = String.valueOf(u.name);
        	users.add(new SelectOption('',u1));
        }
        return users;
    }
	
    public void setSelectedUser(){ 
        
        String test = selectedUser; 
        system.debug(loggingLevel.error, 'selected user is:' + test);
        
    }
    
}

 
Best Answer chosen by Dippan Patel 3
Om PrakashOm Prakash
Hello Dippan,
Jayant explained well but you also need to modify your apex code  users.add(new SelectOption('',u1));
You are using blank value in first parameter which always return selected value as null, it should be  users.add(new SelectOption u1,u1));
Bellow is your modified code which will work.
Changes in your code:
1. In Visualforce page, ActionSupport and one outputPanel added
2. in Apex class, method getActiveUsers modified.
<apex:page controller="PicklistController">
  <apex:form>
      Enter Text: <apex:inputText id="txt1" value="{!textDemo}"  />
    <apex:selectList id="selectedUser" value="{!selectedUser}"  size="1">
        <apex:selectOptions value="{!ActiveUsers}">                                                   
        </apex:selectOptions>
         <apex:actionSupport event="onchange" action="{!setSelectedUser}" rerender="PanelArea"/>   
    </apex:selectList>
    <br/><br/>
     <apex:outputPanel id ="PanelArea">
      {!selectedUser}
     </apex:outputPanel>
  </apex:form>
</apex:page>

public with sharing class PicklistController {
    public String selectedUser {get; set;}
    public String textDemo {get; set;}

    
    public List<SelectOption> getActiveUsers() {
        List<SelectOption> users = new List<SelectOption>();
        List<User> allUsers = [select name from User where isactive = true];
        system.debug(logginglevel.error, +allUsers);
        for(User u : allUsers){
            String u1 = String.valueOf(u.name);
            users.add(new SelectOption(u1,u1));
        }
        return users;
    }
    
    public void setSelectedUser(){ 
        
        String test = selectedUser; 
        system.debug(loggingLevel.error, 'selected user is:' + test);
        
    }
    
}

All Answers

Jayant DasJayant Das
The onchage in your current implementation expects a JS function and thus you don't get the selected value in your controller. Refer to this in the documentation for apex:selectList here -- https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm

To get the selected value, associate a apex:actionSupport to the apex:selectList to invoke the controller method. Something as:
 
<apex:selectList id="selectedUser" value="{!selectedUser}" size="1">
    <apex:actionSupport event="onchange" action="{!setselectedUser}" ...>
Om PrakashOm Prakash
Hello Dippan,
Jayant explained well but you also need to modify your apex code  users.add(new SelectOption('',u1));
You are using blank value in first parameter which always return selected value as null, it should be  users.add(new SelectOption u1,u1));
Bellow is your modified code which will work.
Changes in your code:
1. In Visualforce page, ActionSupport and one outputPanel added
2. in Apex class, method getActiveUsers modified.
<apex:page controller="PicklistController">
  <apex:form>
      Enter Text: <apex:inputText id="txt1" value="{!textDemo}"  />
    <apex:selectList id="selectedUser" value="{!selectedUser}"  size="1">
        <apex:selectOptions value="{!ActiveUsers}">                                                   
        </apex:selectOptions>
         <apex:actionSupport event="onchange" action="{!setSelectedUser}" rerender="PanelArea"/>   
    </apex:selectList>
    <br/><br/>
     <apex:outputPanel id ="PanelArea">
      {!selectedUser}
     </apex:outputPanel>
  </apex:form>
</apex:page>

public with sharing class PicklistController {
    public String selectedUser {get; set;}
    public String textDemo {get; set;}

    
    public List<SelectOption> getActiveUsers() {
        List<SelectOption> users = new List<SelectOption>();
        List<User> allUsers = [select name from User where isactive = true];
        system.debug(logginglevel.error, +allUsers);
        for(User u : allUsers){
            String u1 = String.valueOf(u.name);
            users.add(new SelectOption(u1,u1));
        }
        return users;
    }
    
    public void setSelectedUser(){ 
        
        String test = selectedUser; 
        system.debug(loggingLevel.error, 'selected user is:' + test);
        
    }
    
}
This was selected as the best answer
Dippan Patel 3Dippan Patel 3
Hi Jayant, 

Thank you for your reply. Here is my updated code but I am getting the same null value. 
<apex:page controller="PicklistController">

  <apex:form >
      
	<apex:selectList id="selectedUser" value="{!usersTest}"  size="1" multiselect = "false">
       	<apex:selectOptions value="{!ActiveUsers}"></apex:selectOptions>
        <apex:actionSupport event="onchange" action="{!setselectedUser}" />

	</apex:selectList>
  

  </apex:form>
</apex:page>



public with sharing class PicklistController {

	String user1;

    public List<SelectOption> getActiveUsers() {
        List<SelectOption> users = new List<SelectOption>();
        List<User> allUsers = [select name from User where isactive = true];
        system.debug(logginglevel.error, +allUsers);
        for(User u : allUsers){
        	String u1 = String.valueOf(u.name);
        	users.add(new SelectOption('',u1));
        }
        return users;
    }
    
   public String getUser1() {
            return user1;
    }
            
    public void setUser1(String user1) {
         this.user1 = user1;    
    }
    
    public void setSelectedUser(){ 
        String test = user1;  
        system.debug(loggingLevel.error, 'selected user is:' + test);
    }
    
}

 
Dippan Patel 3Dippan Patel 3
Hi Om, 

Thank you. It worked!