Get logged in user id in Salesforce

Get the currently logged in user id in Apex Triggers, Apex Classes, Visualforce, Lightning Components, Lightning Web Components, Formula Fields.

As far as you progress with configuration or customization with Salesforce, you require to write some complex logic. Performing the logic based upon currently logged in user is also very much required in each business scenario. Here I have gathered ways to get logged in user ids in Formula, Visualforce page, Lightning Component, Apex, Lightning Web Components.

Formulas, Validation Rules, Workflows, Approval Processes

$User.Id

Apex

Id UserId = UserInfo.getUserId();

Visualforce

UserId: {!$User.Id}

Lightning Component

User variable is not directly available on lightning component as of now, this we need to get from lightning component controller.

let UserID = $A.get("$SObjectType.CurrentUser.Id");

Lightning Web Components (LWC)

import USER_ID from '@salesforce/user/Id';