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
StormConsultingStormConsulting 

How to get the current User ID in a Apex class

How can I get the current Salesforce User ID in an Apex class?

Any help is appreciated!

Chris
Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu
You can get the current user id like this.

UserInfo.getUserId() which returns the user id of the current user.

All Answers

hisrinuhisrinu
You can get the current user id like this.

UserInfo.getUserId() which returns the user id of the current user.
This was selected as the best answer
StormConsultingStormConsulting
Thanks a lot, that worked perfectly!
sairam n 5sairam n 5
can we use same syntax to get user id in Triggers too?
CNimmaCNimma
Absolutely. In SOQL queries too!
Venkateswarlu PVenkateswarlu P
for(contact c:conList){
            c.Description = 'New Contact is created by'+''+userInfo.getUserId(); // Get the Id of the current user
        }
malic malisurcmalic malisurc
good answer
Sumit Saini 1Sumit Saini 1
We can get current logged in user full information from predefined methods in apex.
Please check this below link for reference.

https://sfdctechsolutions.blogspot.com/2021/07/get-current-user-information-in-apex.html