<p><h2> Owner Information</h2>
<h3>$User.FirstName </h3>
<br></br>
<h3>$User.LastName </h4>
</p>
-
1Rann A kind request, please start posting the use case and details of what you want, all your questions need formatting and clarity.– RaoCommented Dec 2, 2015 at 19:42
-
The Above code is a part of my lightning component code . I just want to display the current user's first name and last name in the Lightning Application– RannCommented Dec 2, 2015 at 19:44
-
5look at this post for answer to your question : salesforce.stackexchange.com/questions/100951/…– RaoCommented Dec 2, 2015 at 19:47
-
You can vote for this idea to allow global user fields in lightning components reflecting current user information. success.salesforce.com/ideaView?id=0873A000000E7lFQAS– brahmaCommented Jul 8, 2017 at 10:45
1 Answer
You will need to get the information from the server by calling an "AuraEnabled" method on an Apex Class.
Your Apex Class (Controller): RunningUserCtrl
public class RunningUserCtrl {
@AuraEnabled
public static User getCurrentUser() {
User user = [SELECT Id, FirstName, LastName FROM User WHERE Id = :UserInfo.getUserId() LIMIT 1];
return user;
}
}
Your Component: RunningUserCmp
<aura:component controller="RunningUserCtrl"
<aura:attribute name="runningUser" type="User" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<p>User lastname: <ui:outputText value="{!v.runningUser.LastName}"/></p>
<p>User firstname: <ui:outputText value="{!v.runningUser.FirstName}"/></p>
</aura:component>
Look at:<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
This registers an init event handler for the component. init is a predefined event sent to every component. After the component is initialized, the doInit action is called in the component's controller, retrieving the User record.
Your Component's Controller:
({
doInit: function(component, event, helper) {
var action = component.get("c.getCurrentUser"); // method in the apex class
action.setCallback(this, function(a) {
component.set("v.runningUser", a.getReturnValue()); // variable in the component
});
$A.enqueueAction(action);
}
})
-
No query is needed everything exists in the userInfo static methods. You even used that to get the data int he query....– EricCommented Dec 2, 2015 at 21:21
-
Don't we have the ability to print First Name and Last name in Lightning component in a single line of statement , like where we print in a Visualforce page.... @Ardavanmet– RannCommented Dec 2, 2015 at 21:32
-
@Eric is right. If you want to return a few attributes you can use the UserInfo class directly. In the example I returned the whole User record. Commented Dec 2, 2015 at 21:33
-
developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Dec 2, 2015 at 21:34
-