How to Retrieve all Information About the Current User Details Like User Id, Profile Name, UserRole Name, FirstName, LastName, Email, etc.. Using Apex Class in Lightning Component Salesforce | how to get current user id in lightning component Salesforce

5,815 views


Hey guys, today in this post we are going to learn about How to Retrieve all Information About the Current User Details Like User Id, Profile Name, UserRole Name, FirstName, LastName, Email, etc Using Apex Class in Lightning Component Salesforce

Real time scenarios:- Retrieve all Information About the Current User and display data in html table formats.

Files we used in this post example

currentUserDetailApp.app Lightning Application It is used for call the component to preview on browser.

currentUserDetailCmp.cmp

Lightning Component It is used for create a table for display the User Information Detail.

currentUserDetailCmpController.js

Component Controller Javascript It is used for communicate to server side apex method and fetch the User Info details from doInit fuction.
currentUserDetailCtrl.apxc Apex Class Controller It is used for create apex class method to retrieve the User Info Details.

Live Demo

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

how to get current user id in lightning component -- w3web.net

 

You can download file directly from github by Click Here.

 

 

Other related post that would you like to learn in Salesforce

 

Create Lightning Application

Step 1:- Create Lightning Application : currentUserDetailApp.app

From Developer Console >> File >> New >> Lightning Application

currentUserDetailApp.app [Component Application File]

Note:: โ€“ You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.   <aura:application extends="force:slds">
  2.     <c:currentUserDetailCmp/>
  3. </aura:application>

Create Lightning Component

Step 2:- Create Lightning Component : currentUserDetailCmp.cmp

From Developer Console >> File >> New >> Lightning Component

currentUserDetailCmp.cmp [Lightning Component File]

  1.   <aura:component controller="currentUserDetailCtrl">
  2.     <aura:handler name="init" value="this" action="{!c.doInit}"/>
  3.     <aura:attribute name="currentUserList" type="user"/>  
  4.  
  5.     <div class="slds slds-p-horizontal--medium">
  6.  
  7.        <div class="slds-section-title--divider slds-m-bottom--medium"> 
  8.         <h3 class="slds-tile__title slds-text-color--error">
  9.            Current User Information Details
  10.          </h3>
  11.         </div>
  12.  
  13.         <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" style="width:40%; border:1px #ccc solid;" >            
  14.           <tbody>  
  15.             <tr style="background-color:#f3f3f3;">
  16.                 <td><strong>Name</strong></td>
  17.                 <td>{!v.currentUserList.Name}</td>                                    
  18.             </tr>
  19.             <tr>
  20.                 <td><strong>FirstName</strong></td>
  21.                 <td>{!v.currentUserList.FirstName}</td>                                    
  22.             </tr>
  23.             <tr style="background-color:#f3f3f3;">
  24.                 <td><strong>LastName</strong></td>
  25.                 <td>{!v.currentUserList.LastName}</td>                                    
  26.             </tr>
  27.             <tr>
  28.                 <td><strong>Username</strong></td>
  29.                 <td>{!v.currentUserList.Username}</td>                                    
  30.             </tr>
  31.             <tr style="background-color:#f3f3f3;">
  32.                 <td><strong>Country</strong></td>
  33.                 <td>{!v.currentUserList.Country}</td>                                    
  34.             </tr>
  35.             <tr>
  36.                 <td><strong>Email</strong></td>
  37.                 <td>{!v.currentUserList.Email}</td>                                    
  38.             </tr>
  39.             <tr style="background-color:#f3f3f3;">
  40.                 <td><strong>IsActive</strong></td>
  41.                 <td>{!v.currentUserList.IsActive}</td>                                    
  42.             </tr>
  43.             <tr>
  44.                 <td><strong>Alias</strong></td>
  45.                 <td>{!v.currentUserList.Alias}</td>                                    
  46.             </tr>
  47.             <tr style="background-color:#f3f3f3;">
  48.                 <td><strong>TimeZoneSidKey</strong></td>
  49.                 <td>{!v.currentUserList.TimeZoneSidKey}</td>                                    
  50.             </tr>
  51.             <tr>
  52.                 <td><strong>IsPortalEnabled</strong></td>
  53.                 <td>{!v.currentUserList.IsPortalEnabled}</td>                                    
  54.             </tr>     
  55.              <tr style="background-color:#f3f3f3;">
  56.                 <td><strong>Profile Name</strong></td>
  57.                 <td>{!v.currentUserList.Profile.Name}</td>                                    
  58.             </tr> 
  59.              <tr>
  60.                 <td><strong>UserRole Name</strong></td>
  61.                 <td>{!v.currentUserList.UserRole.Name}</td>                                    
  62.             </tr>   
  63.           </tbody>  
  64.         </table> 
  65.  
  66.  <br/><br/>
  67.    <!--Start RelatedTopics Section-->
  68. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  69.  
  70.             <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>
  71.  
  72.             <br/><br/>
  73.             <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>
  74.             <div style="display:block; overflow:hidden;"> 
  75.                 <div style="width: 50%; float:left; display:inline-block">
  76.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  77.                         <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>
  78.                         <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>
  79.                         <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>
  80.                         <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>
  81.                         <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>
  82.                     </ul>
  83.             </div>
  84.  
  85.             <div style="width: 50%; float:left; display:inline-block">
  86.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  87.                         <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>
  88.                         <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>
  89.                         <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>
  90.                         <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>
  91.                         <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>
  92.                     </ul>
  93.                 </div>
  94.                <div style="clear:both;"></div> 
  95.                <br/>
  96.                 <div class="youtubeIcon">
  97.                     <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>
  98.                 </div>
  99.     </div>
  100.  
  101. </div>
  102.  
  103.   <!--End RelatedTopics Section-->
  104.  
  105.     </div>
  106. </aura:component>

Create Component JavaScript Controller

Step 3:- Create Lightning Component : currentUserDetailCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

currentUserDetailCmpController.js [JavaScript Controller]

  1.   ({
  2. 	doInit : function(component, event, helper) {
  3. 	var action = component.get("c.currentUserDetailMethod");
  4.         action.setCallback(this, function(response) {
  5.             var state = response.getState();
  6.             if (state === "SUCCESS") {
  7.                 var results= response.getReturnValue();              
  8.                 component.set("v.currentUserList", results);
  9.             }
  10.         });
  11.         $A.enqueueAction(action);
  12.     },
  13.  
  14. })

Create Apex Class Controller

Step 4:- Create Apex Class : currentUserDetailCtrl.apxc

From Developer Console >> File >> New >> Apex Class

currentUserDetailCtrl.apxc [Apex Class Controller]

  1.   public class currentUserDetailCtrl {
  2.   @AuraEnabled 
  3.     public static USER currentUserDetailMethod(){    
  4.       USER currentUserObj = [SELECT id,Name,FirstName,LastName,Username,Country,Email, IsActive, Alias,TimeZoneSidKey, IsPortalEnabled, Profile.Name,UserRole.Name FROM USER WHERE Id=:userInfo.getUserId()];
  5.         RETURN currentUserObj;
  6.     }
  7. }

how to get current user id in lightning component -- w3web.net

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do I get current user information in LWC?

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

How do I get the current logged in ID in Salesforce?

You can get the ID's of all the currently logged in users by using this global function: UserInfo. getUserId().

Why we use cacheable true in LWC?

To set cacheable=true , a method must only get data, it can't mutate (change) data. Marking a method as cacheable improves your component's performance by quickly showing cached data from client-side storage without waiting for a server trip. When you design your Lightning web components, don't assume a cache duration.

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