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
JaanuJaanu 

Urgent Pls - how to get user name from salesforce id ?

I have 18 char id for the user. But I want to get the User name using the 18 char salesforce user id in Apex class. I tried to use the SOQL query to get the user name... it's exceeding the governer limits.. any other way getting the user name plase .. thanks. 
Best Answer chosen by Jaanu
Deepali KulshresthaDeepali Kulshrestha
Hi Jaanu,

You can use SOQL to retrieve the user name if you have userId. SOQL that needs to be used:
User usr = [Select name From User where Id='0056F000006eynwQAA']; //replace id with actual user id

String userName = usr.Name; //use "usr.Name" to access user name of this record


//above query will run for one user id
//if you have list of userId, you can get the user names from below query
// let's say you have list      "userIDs"       that has all userIds

List<User> usrs =  [Select name From User where Id=: userIDs]; 
//get all user names using below for each loop

for(User u : usrs){
    String userName = u.Name; //retrieve user name of user record
}

I hope you find the above solution helpful.If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

Raj VakatiRaj Vakati
You can get it like this 
 
String userName = [select username from User where id ='18 Digit 1id'].username  ;

 
JaanuJaanu
I had this ... but this SQOL stmt needs to be in for loop ... so, it's exceeding the governer limits... Need another way of getting user name using the 18 char id in APEX class. Pls let me know. 
GovindarajGovindaraj
Hi Jaanu,

If possible, if you have less users or filter condition then you can do like below,
Map<Id,User> userMap = new Map<ID, User> ([SELECT Id, Name FROM User]);
for(User objUser : userMap.values()) {
    system.debug('User Name' +userMap.get(objUser.Id).Name); //fetch user name using id
}
Thanks,
Govindaraj.S
GovindarajGovindaraj
Hi Jaanu,

Please keep this community clean by closing solved cases.

Thanks,
Govindaraj.S
Deepali KulshresthaDeepali Kulshrestha
Hi Jaanu,

You can use SOQL to retrieve the user name if you have userId. SOQL that needs to be used:
User usr = [Select name From User where Id='0056F000006eynwQAA']; //replace id with actual user id

String userName = usr.Name; //use "usr.Name" to access user name of this record


//above query will run for one user id
//if you have list of userId, you can get the user names from below query
// let's say you have list      "userIDs"       that has all userIds

List<User> usrs =  [Select name From User where Id=: userIDs]; 
//get all user names using below for each loop

for(User u : usrs){
    String userName = u.Name; //retrieve user name of user record
}

I hope you find the above solution helpful.If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer