+ Start a Discussion
Ankit SinghalAnkit Singhal 

Creating a dropdown on Visualforce Page with values as Users.

Hi Everyone,


I need to create a dropdown field on Visualforce page which should display the list of Users based on a query.

Then I need to use the chosen  value in my Controller.


How to achieve this??

Plz help its urgent 




Chamil MadusankaChamil Madusanka

Hi Ankit,


Try following code


Visualforce page

<apex:page controller="UserController">
  <apex:form >
      <apex:selectList size="1">
          <apex:selectOptions value="{!UserList}"></apex:selectOptions>



public without sharing class UserController 
    public List<User> UserTemp = new List<User>();
    public UserController()
    public List<SelectOption> UserList
            UserTemp = [Select u.LastName, u.Id, u.FirstName, u.Email From User u];
            UserList = new List<SelectOption>();
            for(User temp : UserTemp)
                UserList.add(new SelectOption(temp.Id, temp.LastName));
            return UserList;


If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.



Hi Ankit


I wish this code should be helpfull for  you. 


Your apex visual force Page 


<apex:page controller="userController">




                      <apex:OutputPanel >

                                 <apex:selectList value="{!selecteduserId}" size="1" multiselect="false"  >
                                          <apex:selectOptions value="{!ListOfUser}" />







Apex class code:-

public with sharing class userController


       //This variable hold the id of  user which is selected by enduser from picklist

        public String selecteduserId {set;get;}


      //This var hold List of user

      public List<SelectOption> getListOfUser()


               List<User> Users = [select id ,Username,name from user] ;

               List<SelectOption> UserOptionList = new List<SelectOption>();

               UserOptionList .add(new SelectOption( ' ' ,'---Select---'));

               for(User u : Users )
                          UserOptionList .add(new SelectOption(u.Id , u.Name));

              retrun UserOptionList ;



  public userController()


                    selecteduserId ='';








i want to give input to sheduler through visulaforce page by dropdownlist, any one can calp me,


Derek Davis 7Derek Davis 7
Thanks Ashlekh! I found your suggestion very helpful!