Get Current User Id Information in Lightning Web Components

Here in this post i am sharing how to get current user id information in different ways in salesforce…

VF Page:

Use {!$User.Id}

Lightning Component:

We have to call apex method to return system.UserInfo.getUserId() now we no longer have to do this method.

In the Lightning Controller, call

the $A.get(“$SObjectType.CurrentUser.Id”) to get the information like below..

var userId = $A.get(“$SObjectType.CurrentUser.Id”);

Console.log(userId);

Ligtning Web Component:

import userId from ‘@salesforce/user/Id’;

 

Example:-

GetUserId.html

<template>
<lightning-card title=”Get User Id” icon-name=”custom:custom14″>

User Id: {userId}

</lightning-card>
</template>

GetUserId.js
import { LightningElement } from ‘lwc’;
import strUserId from ‘@salesforce/user/Id’;

export default class GetUserId extends LightningElement {
userId = strUserId;
}

 GetUserId.js-meta.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata&#8221; fqn=”ShowUserDetail”>
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>

<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>

</LightningComponentBundle>

Thanks
Jayakrishna
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s