Check Permissions in LWC!
Enhance your understanding of user permissions in Salesforce by leveraging the @salesforce/userPermission and @salesforce/customPermission scoped modules.
These modules allow you to easily import and utilize permissions to customize the behavior of your components based on the context user's permissions.
To determine whether a user possesses a specific permission, you can import a static reference to the permission and evaluate its value.
If the value is true or undefined, it indicates whether the user has that permission.
Here's an example of how to import a user permission:
import hasPermission from "@salesforce/userPermission/PermissionName";
In addition to user permissions, you can also work with custom permissions.
Custom permissions might include a namespace, which serves as a unique identifier for customization and package installation within your organization.
When using custom permissions from a managed package, remember to prepend the namespace followed by __ to the permission name.
Here are a few import examples:
import hasPermission from "@salesforce/customPermission/PermissionName";
import hasPermission from "@salesforce/customPermission/namespace__PermissionName";
Feel free to choose a suitable name for the static reference representing your permission.
In the provided example, we used the format has{Permission} to clearly indicate that the reference holds a boolean value.
For example, you can use the following code snippet to check if the current user possesses the ViewSetup standard permission:
const hasViewSetup = hasPermission;
import { LightningElement } from 'lwc';
import hasViewSetup from '@salesforce/userPermission/ViewSetup';
export default class App extends LightingElement {
get isSetupEnabled() {
return hasViewSetup;
}
openSetup(e) {...}
}
Follow Us