Get Current User Id in Salesforce

Quick Code Snippets of how to get Current logged in user ids in Salesforce

Apex

System.debug('Current User Id - '+UserInfo.getUserId());

Visualforce

<apex:page>
  <h1>Visualforce Page</h1> 
   <p>UserId: {!$User.Id}</p>
   <p>User Email: {!$User.Email}</p>
</apex:page>

Aura Component

let currentUser = $A.get("$SObjectType.CurrentUser.Id");
Console.log(currentUser);

Lightning Web Components (LWC)

Using wire

import USER_ID from '@salesforce/user/Id';
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

Formula Fields

$User.Id

6 comments

  1. Very useful info. Thanks.

    For Formula fields in a Lightning Flow Builder formula, use CASESAFEID($User.Id) if you are comparing to id values returned by the GetRecords Element. $User.ID on it’s own returns a 15 character id, not the newer 18 character id.

Leave a Reply to Ashima Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.