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
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]
<aura:application extends="force:slds">
<c:currentUserDetailCmp/>
</aura:application>
Create Lightning Component
Step 2:- Create Lightning Component : currentUserDetailCmp.cmp
From Developer Console >> File >> New >> Lightning Component
currentUserDetailCmp.cmp [Lightning Component File]
|
<aura:component controller="currentUserDetailCtrl">
<aura:handler name="init" value="this" action="{!c.doInit}"/>
<aura:attribute name="currentUserList" type="user"/>
<div class="slds slds-p-horizontal--medium">
<div class="slds-section-title--divider slds-m-bottom--medium">
<h3 class="slds-tile__title slds-text-color--error">
Current User Information Details
</h3>
</div>
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" style="width:40%; border:1px #ccc solid;" >
<tbody>
<tr style="background-color:#f3f3f3;">
<td><strong>Name</strong></td>
<td>{!v.currentUserList.Name}</td>
</tr>
<tr>
<td><strong>FirstName</strong></td>
<td>{!v.currentUserList.FirstName}</td>
</tr>
<tr style="background-color:#f3f3f3;">
<td><strong>LastName</strong></td>
<td>{!v.currentUserList.LastName}</td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>{!v.currentUserList.Username}</td>
</tr>
<tr style="background-color:#f3f3f3;">
<td><strong>Country</strong></td>
<td>{!v.currentUserList.Country}</td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>{!v.currentUserList.Email}</td>
</tr>
<tr style="background-color:#f3f3f3;">
<td><strong>IsActive</strong></td>
<td>{!v.currentUserList.IsActive}</td>
</tr>
<tr>
<td><strong>Alias</strong></td>
<td>{!v.currentUserList.Alias}</td>
</tr>
<tr style="background-color:#f3f3f3;">
<td><strong>TimeZoneSidKey</strong></td>
<td>{!v.currentUserList.TimeZoneSidKey}</td>
</tr>
<tr>
<td><strong>IsPortalEnabled</strong></td>
<td>{!v.currentUserList.IsPortalEnabled}</td>
</tr>
<tr style="background-color:#f3f3f3;">
<td><strong>Profile Name</strong></td>
<td>{!v.currentUserList.Profile.Name}</td>
</tr>
<tr>
<td><strong>UserRole Name</strong></td>
<td>{!v.currentUserList.UserRole.Name}</td>
</tr>
</tbody>
</table>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</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]
|
({
doInit : function(component, event, helper) {
var action = component.get("c.currentUserDetailMethod");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var results= response.getReturnValue();
component.set("v.currentUserList", results);
}
});
$A.enqueueAction(action);
},
})
Create Apex Class Controller
Step 4:- Create Apex Class : currentUserDetailCtrl.apxc
From Developer Console >> File >> New >> Apex Class
currentUserDetailCtrl.apxc [Apex Class Controller]
|
public class currentUserDetailCtrl {
@AuraEnabled
public static USER currentUserDetailMethod(){
USER currentUserObj = [SELECT id,Name,FirstName,LastName,Username,Country,Email, IsActive, Alias,TimeZoneSidKey, IsPortalEnabled, Profile.Name,UserRole.Name FROM USER WHERE Id=:userInfo.getUserId()];
RETURN currentUserObj;
}
}
Further post that would you like to learn in Salesforce
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
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 |