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
vish hvish h 

How to get the user details from ID stored in string format?

Hi,

I'm new to Salesforce and I want to know how can I get back the user details that I stored ID in string data type. Ex

public class vSFDCuseinfo{
public string auserid {get;set;}
public string aName{get;set;}
public vSFDCuseinfo (){
     
      auserid= UserInfo.getUserId();
      aName = auserID.username;    // how to get the username (firstname, lastname etc) from ID stored in string format? 
      

}
Thanks 

Vish
Best Answer chosen by vish h
ClintLeeClintLee
You'll need to query the User record using the Id.

Here's an example:
// query the User 
User u = [select Id, Username from User where Id = :UserInfo.getUserId()];

// access the User's fields using the queried record.
System.debug('Username is ' + u.Username);
System.debug('User Id is ' + u.Id);

Hope that helps,

Clint

All Answers

ClintLeeClintLee
You'll need to query the User record using the Id.

Here's an example:
// query the User 
User u = [select Id, Username from User where Id = :UserInfo.getUserId()];

// access the User's fields using the queried record.
System.debug('Username is ' + u.Username);
System.debug('User Id is ' + u.Id);

Hope that helps,

Clint
This was selected as the best answer
vish hvish h
Thank you Clint !!! It worked..