@salesforce/userPermission
and
@salesforce/customPermission
APIs.
Firstly we need to import the static reference to the permissions like
below.
Import User Permissions
import hasPermission from '@salesforce/userPermission/PermissionName';
Import Custom Permissions
import hasPermission from '@salesforce/customPermission/PermissionName';
import hasPermission from '@salesforce/customPermission/namespace__PermissionName';
Sample Code: check user permission in the lightning web component
In the below example we are checking if the user has View All Data
permission.
JS Code
// myLwc.js import { LightningElement } from 'lwc'; import hasViewAllData from '@salesforce/userPermission/ViewAllData'; export default class MyLwc extends LightingElement { get hasViewAllData() { return hasViewAllData; } }
hasViewAllData
method in HTML as below. The contents inside that template will be visible only if the user has that permission.
HTML Code
<template> <template if:true={hasViewAllData}> This section is visible only if user has view all data permission </template> </template>
Sample Code: check custom permission in the lightning web component
In this example, I have created custom permission
named hasViewPackageSetupPage we will use this to check if the user can
access the setup page of the managed package.
JS Code
// myLwc.js import { LightningElement } from 'lwc'; import hasViewPackageSetupPage from '@salesforce/customPermission/ViewPackageSetupPage'; export default class MyLwc extends LightingElement { get hasViewPackageSetupPage() { return hasViewPackageSetupPage; } }
HTML Code
<template> <template if:true={hasViewPackageSetupPage}> This section is visible only if user has hasViewPackageSetupPage custom permission </template> </template>
Similarly, you can check other permissions directly into a lightning web component. This has definitely made our life easier.
No comments :
Post a Comment
Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com