How to Use Custom Permission in Lightning Web Component (Summer 20 Feature)

It seems obvious needed for many developers but only recently Salesforce added the ability to access custom permissions in Lightning Web Component.

Previously we needed to use some workaround like formula fields or server functions. 

With this new feature in summer 20, the workarounds are no longer required.


Simply create the custom permission (if you don't have it yet) and assign it to the relevant profiles.


In your LWC JS code import the custom permission from '@salesforce/customPermission'


1
2
3
4
5
6
7
8
import { LightningElement } from 'lwc';
import viewLWCPermission from '@salesforce/customPermission/View_LWC_Content';
 
export default class LWCCustomPermission extends LightningElement {
    get isUserCanAccessLWC() {
        return viewLWCPermission;
    }
}


Finally in the html code use the permission


1
2
3
4
5
<template>
    <template if:true={isUserCanAccessLWC}>
        Great! You can access this content
    </template>
</template>



😏

No comments:

Post a Comment

Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...