Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
A screenshot showing Apex code examples for getting the current logged in user ID in Salesforce.
Apex

Get logged in user id in Salesforce

The short answer

Salesforce has its own syntax for the currently logged-in user ID in each development context. You can read the user ID declaratively in formulas and Visualforce, or programmatically in Apex, Aura Lightning Components, and Lightning Web Components (LWC).

Key takeaways Use $User.Id to access the logged-in user ID in formulas, validation rules, workflows, and approval processes. Call UserInfo.getUserId() to retrieve the current user ID in Apex. Reference {!$User.Id} in Visualforce page markup to display the current user ID. Fetch the user ID in a Lightning Component controller using $A.get("$SObjectType.CurrentUser.Id"). Import USER_ID from '@salesforce/user/Id' to access the user ID in Lightning Web Components (LWC).

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';

Frequently asked questions

How do you get the logged in user ID in Apex?

You can get the current user ID in Apex by calling Id UserId = UserInfo.getUserId().

How do you get the current user ID in LWC?

In Lightning Web Components (LWC), import the user ID using import USER_ID from '@salesforce/user/Id'.

How do you get the current user ID in an Aura Lightning component?

Because the user variable is not directly available on the component markup, retrieve it in the JavaScript controller using let UserID = $A.get("$SObjectType.CurrentUser.Id").

How do you get the logged in user ID in formulas and validation rules?

Reference the global variable $User.Id in formula fields, validation rules, workflow rules, and approval processes.

How do you get the current user ID in Visualforce?

Reference {!$User.Id} directly in your Visualforce page.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment