To get current user id in LWC we need to import the "@salesforce/user/Id" Salesforce module.
Syntax :-
import userId from '@salesforce/user/Id';
Sample Code :-
showUserId.html
<template>
<lightning-card title="Show User Detail" icon-name="custom:custom15">
<div class="slds-m-around_medium">
<p>User Id: {userId} </p>
</div>
</lightning-card>
</template>
<lightning-card title="Show User Detail" icon-name="custom:custom15">
<div class="slds-m-around_medium">
<p>User Id: {userId} </p>
</div>
</lightning-card>
</template>
showUserId.js
import { LightningElement } from 'lwc';
import strUserId from '@salesforce/user/Id';
export default class ShowUserDetail extends LightningElement {
userId = strUserId;
}
import strUserId from '@salesforce/user/Id';
export default class ShowUserDetail extends LightningElement {
userId = strUserId;
}
showUserId.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="ShowUserDetail">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="ShowUserDetail">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output :-
Please check our old post on Lightning Web Components:-
Feel free to post your feedback or question.
Thanks,
Amit Chaudhary
Thanks for sharing this post.
ReplyDeleteReally helpful to me.
Thanks for your feedback
DeleteHello Amit,
ReplyDeleteI want to implement a Login page Community with the help of LWC. Please guide.
Thanks,
Deepak
is there a way to get the accountId off the user record so we can lookup what account they are tied to?
ReplyDeleteOkay, the answer is yes, spelling is crucial AccountId get this.
Deletehow?
DeleteHi Amit,
ReplyDeleteI created LWC using vscode but I unable to find the ".css" of particular LWC.
Could you please tell where I can find ".css" of LWC ?
Thanks ,
Vicky
Hi vicky, you have to create the css file...it doesnt come by default
DeleteHo to fetch user's Business Unit and email in lwc
ReplyDelete