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
Isaac GomolkaIsaac Gomolka 

Lookup Field for Users on Visualforce Page

Hi, I was wondering how to get a Lookup field for Users onto a visualforce page. I tried finding something on this, but nobody uses it with Users, they use accounts or opportunities. I want to be able to have an inputField with the lookup button thing next to it so whoever is using my visualforce page can click on it and lookup a User that we have in Salesforce and select it. If you could also help explain how I would then be able to pull information from the user selected, thatd be a great help. By that i mean like I'm supposed to be copying information of a User that someone inputs to use to create a new user. Any help or explanations would be very very helpful. Thanks!!!
Rahul.MishraRahul.Mishra
Hi Isaac Gomolka,

Normally we user apex:input field to bind the field so that type of field automatically reflected on page, howver if you want to create a lookup to user then you can create reference for a field where  lookup to user exist, I am taking the example of Manager field on user object, manager field is lookup to user on user object.
 
Class:

public class lookUpController{
    
    Public User usr {get;set;}
    public lookUpController() {
        usr = new User();
    }
    
}



Page:
<apex:page controller="lookUpController1">
 <apex:form >
  <apex:pageBlock >
   <apex:inputField value="{!usr.ManagerId}"/>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Here usr is type of get set so the value selected from the page will be reflected in usr.ManagerId field, you can pull additional information on basis of it.

For more detail use this : https://developer.salesforce.com/forums/?id=906F000000094YKIAY

Mark answer as solved if it does help you.

 
Isaac GomolkaIsaac Gomolka
Thanks for the response Rahul! That does work, but is that what a lookup field is? I'm still very new to Salesforce, so sorry if i sound dumb. But i thought a lookup field would be like a field with this little magnifying glass picture next to it User-added image that u can click and choose a user from a list of all users in the sandbox or something like that. Is that possible? Cause I need to take the user that someone types in that inputfield and copy over a bunch of that looked up user's info onto a newly created user. So does that method pull the entire User's info or only the field i assign it to be? Thanks again for your help. Really Really appreciate it.
Rahul.MishraRahul.Mishra
Hi Isaac,
Yes that should be the lookup field, if you open in classic then could see magnifying glass picture while in lightning it is bit different. 

Thanks,
Rahul
Isaac GomolkaIsaac Gomolka
Rahul,
So I guess it does bring up the magnifying glass and makes it a lookup field. I was trying it with a different field then ManagerID. Why does it only work for some fields and not others? And if you know, if I, say, set it to ManagerId and lookup a User and select it. Is there a way to store that User? In the sense that like I need that whoever is using the page to use that lookupfield to select a User, and then in my apex code or whatever use many fields and things from that looked up user. For example, if i look up the name Matt Smith and select it, in my code is there a way to use like usr.OtherFields and get info from Matt Smith's User information besides the ManagerID(or whatever the lookup field refers to)?

Thanks again for your help! I really appreciate it.
-Isaac
Rahul.MishraRahul.Mishra
Hi Isaac,

It will work for only the fields which are of lookup to user type, ultimatly we are using lookup from a field a same type, also whatever the you will select here will get bind to apex veriable usr which is of type get; set;, you can further utilize the value using usr.Manager, it will return the id of the selected user.

Mark solved if it does help you
Isaac GomolkaIsaac Gomolka
Hey Rahul,
When I try using usr.Field(field being any field besides ManagerID) it doesn't return anything. However when i do use usr.ManagerID it does return the ID of the user selected. Do you know how I would be able to get all the other information from that selected user? Pardon me again im not very good at Salesforce yet, this is my first semi-big project I've been put on as an intern. If i had to guess I would assume I would need some kind of SOQL statement or something that searches Users using the ID given to me from the inputfield, and assign that user to a user variable so I can get any of their fields. Would this be correct? And if so would you have any idea what that SOQL statement would be? Can't thank you enough for all the help!!!

Thanks,
Isaac