How to get current User Id using Apex in Salesforce?

How to get current User Id using Apex in Salesforce?

UserInfo.getUserId() – returns the current User Id.

Sample Code:

User objUser = new User();
objUser = [ SELECT Phone, Id FROM User WHERE Id = : UserInfo.getUserId() ];
system.debug( 'User Id ' + objUser.Id );
system.debug( 'Phone ' + objUser.Phone);

The above code will return the current user’s Phone and Id who execute this code. Execute the code in Anonymous window.

Leave a Reply