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
Chance AllredChance Allred 

How can I query and display data of a specific user?

Hello, I am fairly new and am experimenting with Apex & Visualforce.

I managed to figure out how to display a list of all the users in my org, but I can't figure out how to display a specific user.

Apex Class:
public class UserResultsController {
    
    public List<String>getListOfUsers(){
		List<string> ourUsers = new list<string>();
			
        	for(User a:[SELECT Name FROM User]){
				ourUsers.add(a.Name);
			}
        
		return ourUsers;
	}
 
}
Apex Page:
<apex:page controller="UserResultsController" >
    
   <apex:repeat value="{!ListOfUsers}" var="usl"> 
       <apex:outputText value="{!usl}"/> <br/> 
   </apex:repeat>
        
</apex:page>

Is there a way to either populate the data about a specific user using their User Id or doing a query for a specific user?

I know I can display the current logged in user data by using: {!$User.FirstName} for instance, but I want to display a specific user, not the current logged in user.

Help is greatly appreciated. 
Best Answer chosen by Chance Allred
KdKomalKdKomal
Hi Chance,

Please refer the below code with the help of which you can search for a User and its corresponding details will be shown :

VF Page :-
<apex:page controller="UserResultsController" >
   <apex:form id="formId">
       <apex:pageBlock >
           <apex:pageBlockSection columns="2">
           
               <apex:pageBlockSectionItem >
                   <apex:outputLabel for="usrName">User Name </apex:outputLabel>    
                   <apex:inputText id="usrName" value="{!usrName}" label="Get User"/>
                   
               </apex:pageBlockSectionItem>
                   <apex:commandButton value="Get User" action="{!fetchUsr}" reRender="usrdetail,formId"/>
               <apex:pageBlockSectionItem >
                   
               </apex:pageBlockSectionItem>
           </apex:pageBlockSection>    
       </apex:pageBlock>
   </apex:form>
   <apex:pageBlock id="usrdetail">
       <apex:pageBlockSection columns="1">
           <apex:pageBlockSectionItem >
               <apex:pageBlockTable value="{!ListOfUsers}" var="usl">
                   <apex:column value="{!usl.name}"/>
                   <apex:column value="{!usl.username}"/>
               </apex:pageBlockTable>
           </apex:pageBlockSectionItem>
       </apex:pageBlockSection>
   </apex:pageBlock>        
</apex:page>

Controller :-
public class UserResultsController {
    
    public String usrName{get;set;}
    public List<User> ListOfUsers{get;set;}
    
    public UserResultsController(){
        ListOfUsers = new list<User>();
        for(User a:[SELECT Name,userName FROM User]){
                ListOfUsers.add(a);
            }
        
    } 
    
    
    
    public pagereference fetchUsr(){
        String uName = '%' + usrName + '%';
        system.debug('uName '+uName );
        ListOfUsers = new list<User>();
        ListOfUsers  = [Select Id, name, Username from user where name like: uName];
        system.debug('ListOfUsers  '+ListOfUsers  );
        return null;
    }
 
}

All Answers

Raj VakatiRaj Vakati
Store those a specific user Id in custom setting or custom metadata data and pass it to the where condition in controller 
 
public class UserResultsController {
    
    public List<String>getListOfUsers(){
		List<string> ourUsers = new list<string>();
			
        	for(User a:[SELECT Name FROM User where ID IN :< VALUES FROM SETTINGS>]){
				ourUsers.add(a.Name);
			}
        
		return ourUsers;
	}
 
}

 
Prashant Pandey07Prashant Pandey07
+Raj..You can also simply add the filter in the user query..
 
public class UserResultsController {
    
    public List<String>getListOfUsers(){
		List<string> ourUsers = new list<string>();
			
        	for(User a:[SELECT Name FROM User Where username='user's name']){
				ourUsers.add(a.Name);
			}
        
		return ourUsers;
	}
 
}

--
Thanks,
Prashant
KdKomalKdKomal
Hi Chance,

Please refer the below code with the help of which you can search for a User and its corresponding details will be shown :

VF Page :-
<apex:page controller="UserResultsController" >
   <apex:form id="formId">
       <apex:pageBlock >
           <apex:pageBlockSection columns="2">
           
               <apex:pageBlockSectionItem >
                   <apex:outputLabel for="usrName">User Name </apex:outputLabel>    
                   <apex:inputText id="usrName" value="{!usrName}" label="Get User"/>
                   
               </apex:pageBlockSectionItem>
                   <apex:commandButton value="Get User" action="{!fetchUsr}" reRender="usrdetail,formId"/>
               <apex:pageBlockSectionItem >
                   
               </apex:pageBlockSectionItem>
           </apex:pageBlockSection>    
       </apex:pageBlock>
   </apex:form>
   <apex:pageBlock id="usrdetail">
       <apex:pageBlockSection columns="1">
           <apex:pageBlockSectionItem >
               <apex:pageBlockTable value="{!ListOfUsers}" var="usl">
                   <apex:column value="{!usl.name}"/>
                   <apex:column value="{!usl.username}"/>
               </apex:pageBlockTable>
           </apex:pageBlockSectionItem>
       </apex:pageBlockSection>
   </apex:pageBlock>        
</apex:page>

Controller :-
public class UserResultsController {
    
    public String usrName{get;set;}
    public List<User> ListOfUsers{get;set;}
    
    public UserResultsController(){
        ListOfUsers = new list<User>();
        for(User a:[SELECT Name,userName FROM User]){
                ListOfUsers.add(a);
            }
        
    } 
    
    
    
    public pagereference fetchUsr(){
        String uName = '%' + usrName + '%';
        system.debug('uName '+uName );
        ListOfUsers = new list<User>();
        ListOfUsers  = [Select Id, name, Username from user where name like: uName];
        system.debug('ListOfUsers  '+ListOfUsers  );
        return null;
    }
 
}

This was selected as the best answer
Chance AllredChance Allred
@kd Komal, you are the man! Thank you so much!
KdKomalKdKomal
Hi chance,
Glad I could help you out.