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
Other related post that would you like to learn
Step 1:- Create Lightning Application : currentUserDetailApp.app
From Developer Console >> File >> New >> Lightning Application
currentUserDetailApp.app [Component Application File]
1 2 3 |
<aura:application extends="force:slds"> <c:currentUserDetailCmp/> </aura:application> |
Step 2:- Create Lightning Component : currentUserDetailCmp.cmp
From Developer Console >> File >> New >> Lightning Component
currentUserDetailCmp.cmp [Lightning Component File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<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> </div> </aura:component> |
Step 3:- Create Lightning Component : currentUserDetailCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
currentUserDetailCmpController.js [JavaScript Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
({ 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); }, }) |
Step 4:- Create Apex Class : currentUserDetailCtrl.apxc
From Developer Console >> File >> New >> Apex Class
currentUserDetailCtrl.apxc [Apex Class Controller]
1 2 3 4 5 6 7 |
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 LWC
Nice Blog
5 Internet Approval Site (Do Follow Backlinks)