GETTING CURRENT USER ID IN SALESFORCE

Quick Code Snippets of how to get Current logged in User Id’s in Salesforce.

Apex Class :

  • Syntax:
  • System.debug(‘Current User Id – ‘+UserInfo.getUserId());

VisualForce Page :

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

Aura Component :

  • Syntax:
  • var currentuserId = $A.get(“$SObjectType.CurrentUser.Id”);
  • Console.log(currentuserId);

Formula Fields :

  • Syntax:
  • $User.Id

Lightning Web Components (LWC)

  • Syntax:
  • Import USER_ID from ‘@salesforce/user/Id’;
  • Import NAME_FIELD from ‘@salesforce/schema/User.Name’;
  • Import EMAIL_FIELD from ‘@salesforce/schema/User.Email’;
<strong>Nancy Gupta</strong>
Nancy Gupta

Salesforce Developer/Consultant

Leave a Reply