Salesforce Quick Tips

Salesforce Quick Tip – How to Get the Running User’s Id in an LWC

2 min read

Subscribe on YouTube

Why This Is Useful

You want your Lightning Web Component to know who’s looking at it. Maybe you’re greeting them by name, maybe you’re showing their own records, maybe you’re checking something about them before rendering.

You’d think that needs an Apex controller. It doesn’t. It’s one import.


The Code

JavaScript
import { LightningElement } from 'lwc';
import USER_ID from '@salesforce/user/Id';

export default class QuickTipLwc extends LightningElement {
    userId = USER_ID;
}

And in your template:

HTML
<template>
    <p>The running user Id is {userId}</p>
</template>

That’s it. Drop it on a Lightning record page and there’s the Id.

No Apex controller, no wire adapter, no server round trip. Salesforce injects the value at build time.


Now Go Get The Rest Of Their Details

An Id on its own isn’t much use. Pair it with getRecord from the UI Record API and you can pull whatever you need about that user, still without writing a line of Apex:

JavaScript
import { LightningElement, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import USER_ID from '@salesforce/user/Id';
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

export default class GreetTheUser extends LightningElement {

    @wire(getRecord, { recordId: USER_ID, fields: [NAME_FIELD, EMAIL_FIELD] })
    user;

    get name()  { return getFieldValue(this.user.data, NAME_FIELD); }
    get email() { return getFieldValue(this.user.data, EMAIL_FIELD); }
}

Now you can greet them properly: “Hey Matt, how’s it going?”

The nice part is that Lightning Data Service caches this and respects field level security automatically. Your user can only see fields they’re allowed to see, and you didn’t have to write a single permission check.


JavaScript
import USER_ID from '@salesforce/user/Id';
import IS_GUEST from '@salesforce/user/isGuest';
import hasPermission from '@salesforce/userPermission/ViewAllData';
import hasCustomPerm from '@salesforce/customPermission/My_Custom_Permission';

isGuest is genuinely handy if your component runs in an Experience Cloud site and needs to behave differently for logged out visitors.

The custom permission import is the tidy way to gate functionality without querying anything. Full list is in the LWC modules documentation.


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture