LWC to Get Logged in User Id, Name, Email, IsActive, Alias details without apex class Uses of ‘uiRecordApi’ property and display the current User detail on lightning component in Salesforce Lightning Web Component (LWC) | How to get current user detail in lwc

16,360 views


Hey guys, today in this post we are going to learn about how to Get Logged in User Id, Name, Email, IsActive, Alias details without apex class Uses of ‘uiRecordApi’ property and display the current User detail on lightning component in Salesforce Lightning Web Component (LWC).

To get the current user information, use the @salesforce/user scoped module in LWC.

In this post we used the supported properties are Id, User.Name, User.Email, User.IsActive, User.Alias

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

Files we used in this post example

lwcCurrentUserInfo.html Lightning Web Component HTML Template HTML file for used to write HTML element for display the current user details.
lwcCurrentUserInfo.js LWC JavaScript File It is importing the ‘uiRecordApil property function that is getting the Current User details from database in LWC.
lwcCurrentUserInfo.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage..

Final Output

LWC get current user Id for current User without apex method -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

  • Find the below steps

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➡ lwcCurrentUserInfo.html

SFDX:Lightning Web Component ➡ New ➡ lwcCurrentUserInfo.html

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

lwcCurrentUserInfo.html [Lightning Web Component HTML]

  1.    <template>
  2. <lightning-card title="How to get current user details in LWC" icon-name="standard:user">
  3. <div class="slds-m-around_medium">
  4.  
  5.     <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" width="100%" border="1" bordercolor="#ddd" cellpadding="5" cellspacing="0" style="border-collapse:collapse; font-weight:bold;">
  6.         <tbody> 
  7.         <tr>
  8.         <td valign="top" width="50%" style="background:#eee;">User Id</td>
  9.         <td valign="top" width="50%" style="color:#ff8000;">{userId}</td>        
  10.         </tr>
  11.         <tr>
  12.         <td style="background:#eee;">User Name</td>
  13.         <td style="color:#ff8000;">{currentUserName}</td>
  14.         </tr>
  15.         <tr>
  16.         <td style="background:#eee;">User Email</td>
  17.         <td style="color:#ff8000;"> {currentUserEmailId}</td>
  18.         </tr>
  19.         <tr>
  20.             <td style="background:#eee;">IsActive</td>
  21.             <td style="color:#ff8000;"> {currentIsActive}</td>
  22.             </tr>
  23.         <tr>
  24.         <td style="background:#eee;">Alias Name</td>
  25.         <td style="color:#ff8000;"> {currentUserAlias}</td>
  26.         </tr>
  27.         </tbody>
  28.     </table>
  29.  
  30.  
  31. </div>
  32. <br/><br/>
  33.    <!--Start RelatedTopics Section-->
  34. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  35.  
  36.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  37.  
  38.             <br/><br/>
  39.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  40.             <div style="display:block; overflow:hidden;"> 
  41.                 <div style="width: 50%; float:left; display:inline-block">
  42.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  43.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  44.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  45.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  46.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  47.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  48.                     </ul>
  49.             </div>
  50.  
  51.             <div style="width: 50%; float:left; display:inline-block">
  52.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  53.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  54.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  55.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  56.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  57.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  58.                     </ul>
  59.                 </div>
  60.                <div style="clear:both;"></div> 
  61.                <br/>
  62.                 <div class="youtubeIcon">
  63.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  64.                 </div>
  65.     </div>
  66.  
  67. </div>
  68.  
  69.   <!--End RelatedTopics Section-->
  70.  
  71.    </lightning-card> 
  72. </template>
  73.  
  74. </lightning-card>
  75. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➡ lwcCurrentUserInfo.js

SFDX:Lightning Web Component ➡ New ➡ lwcCurrentUserInfo.js

lwcCurrentUserInfo.js [LWC JavaScript File]

  1.    import { LightningElement,wire } from 'lwc';
  2. import Id from '@salesforce/user/Id';
  3. import { getRecord } from 'lightning/uiRecordApi';
  4. import UserNameFld from '@salesforce/schema/User.Name';
  5. import userEmailFld from '@salesforce/schema/User.Email';
  6. import userIsActiveFld from '@salesforce/schema/User.IsActive';
  7. import userAliasFld from '@salesforce/schema/User.Alias';
  8.  
  9.  
  10. export default class LwcCurrentUserInfo extends LightningElement {
  11.  
  12.     userId = Id;
  13.     currentUserName;
  14.     currentUserEmailId;
  15.     currentIsActive;
  16.     currentUserAlias;
  17.     error;
  18.     @wire(getRecord, { recordId: Id, fields: [UserNameFld, userEmailFld, userIsActiveFld, userAliasFld ]}) 
  19.     userDetails({error, data}) {
  20.         if (data) {
  21.             this.currentUserName = data.fields.Name.value;
  22.             this.currentUserEmailId = data.fields.Email.value;
  23.             this.currentIsActive = data.fields.IsActive.value;
  24.             this.currentUserAlias = data.fields.Alias.value;
  25.         } else if (error) {
  26.             this.error = error ;
  27.         }
  28.     }
  29.  
  30. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➡ lwcCurrentUserInfo.js-meta.xml

SFDX:Lightning Web Component ➡ New >> lwcCurrentUserInfo.js-meta.xml

lwcCurrentUserInfo.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Create Lightning Application

Step 4:- Create Lightning Application ➡ lwcCurrentUserInfoApp.app

From Developer Console ➡ File ➡ New ➡ Lightning Application

lwcCurrentUserInfoApp.app [Lightning Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:lwcCurrentUserInfo/>
  3. </aura:application>

LWC Get user name for current User in LWC -- w3web.net

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

How do I find the current user ID in Apex Salesforce?

To get the current User Id in Apex Class, we can use getUserId() method of UserInfo System Class.

What is Userinfo getUserId ()?

Returns the current user's email address. getUserId() Returns the context user's ID. getUserName() Returns the context user's login name.

What is profile ID in Salesforce?

Salesforce Profile IDs are 15-digit identification codes that are unique from others within the system and are used internally to distinguish one profile from another even if all other details between the two are identical.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment