Check User Permissions for Lightning Web Components

Share on facebook
Share on twitter
Share on linkedin
User permissions specify what tasks users can perform and what features users can access. For example, users with the “View Setup and Configuration” permission can view Setup pages, and users with the “API Enabled” permission can access any Salesforce API.

User Permissions:

  • User permissions specify what tasks users can perform and what features users can access
  • For example, users with the “View Setup and Configuration” permission can view Setup pages, and users with the “API Enabled” permission can access any Salesforce API
  • You can enable user permissions in permission sets and custom profiles.
  • In permission sets and the enhanced profile user interface, these permissions—as well as their descriptions—are listed in the App Permissions or System Permissions pages
  • In the original profile user interface, user permissions are listed under Administrative Permissions and General User Permissions
  • To view permissions and their descriptions, from Setup, enter Permission Setsin the Quick Find box, then select Permission Sets, then select or create a permission set. Then from the Permission Set Overview page, click App Permissions or System Permissions.

Lightning Web Components:

  • Lightning Web Components is a new programming model for building Lightning components.
  • It leverages the web standards breakthroughs of the last five years, can co-exist and interoperate with the original Aura programming model, and delivers unparalleled performance.

Any Time you struggled to get a user permission in LWC Component? Simple Solution We have in Salesforce now.

  • Customize a component’s behaviour based on whether the current user has a specific permission.
  • To check a user’s permission assignment, import Salesforce permissions from @salesforce/userPermissionand @salesforce/customPermission scoped modules.

Where we can use it? Any Restrictions?

  • Lightning Web components in Lightning Experience in any version.

Why Do We need permission in LWC?

  • Develop Lightning web components to behave a certain way based on the current user’s permissions.

Ways to Implement this feature in code:

  • To check whether a user has a permission, import a static reference to the permission and evaluate whether it is trueor false.

import hasPermission from ‘@salesforce/userPermission/PermissionName’;

We chose the format hasPermission to indicate that the reference contains a boolean.

This sample code checks whether the current user has the View Setup standard permission.

// example.js

import { LightningElement } from ‘lwc’;

import hasViewSetup from ‘@salesforce/userPermission/ViewSetup’;

export default class Example extends LightingElement {

get isSetupEnabled() {

return hasViewSetup;

}

openSetup(e) {…}

}

//Example.html

<template>

<setup-panel-group>

<setup-button disabled={isSetupEnabled} onclick={openSetup}></setup-button>

</setup-panel-group>

</template>

With Namespace:

  • Custom permissions can include a namespace. Orgs use namespaces as unique identifiers for their own customization and packages.
  • If the custom permission has a namespace, you must prepend the namespace followed by __to the permission name.

import hasPermission from ‘@salesforce/customPermission/PermissionName’;

import hasPermission from ‘@salesforce/customPermission/namespace__PermissionName’;

This sample code checks whether the current user has the ViewReport custom permission installed from a managed package with the mst namespace.

// example.js

import { LightningElement } from ‘lwc’;

import hasViewReport from ‘@salesforce/customPermission/ mst __ViewReport’;

export default class Example extends LightingElement {

get isReportVisible() {

return hasViewReport;

}

}

//example.html

<template>

<common-view></common-view>

<template if:true={isReportVisible}>

<c-expense-report></c-expense-report>

</template>

</template>

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.