6

I'm trying to access a custom permission in a Lightning Web Component.

This is the js code in my managed package development org:

import { LightningElement } from 'lwc';
import hasCustomPermission from '@salesforce/customPermission/My_Custom_Permission';

export default class MyLwcComponent extends LightningElement {
    
    get isCustomPermissionEnabled() {
        return hasCustomPermission;
    {
}

This code works fine in the development org and correctly gives me My_Custom_Permission.

When I release the managed package and install it in subscriber org, I get the following error:

This page has an error. You might just need to refresh it. Compilation Failure myLwcComponent.js:0,0 : LWC1504: Invalid module id "MY_NAMESPACE__My_Custom_Permission" for type "customPermission". Explicit use of namespace "MY_NAMESPACE" in file "myLwcComponent.js" is prohibited. Use default namespace "c" instead.

Any idea how to get this working?

It's obviously trying to namespace my custom permission in the subscriber org.

In the managed package development org, I've also tried prefixing the custom permission with both the default namespace 'c' and my namespace 'MY_NAMESPACE', but get errors.

e.g.

For import hasCustomPermission from '@salesforce/customPermission/MY_NAMESPACE__My_Custom_Permission';

Error: LWC1504: Invalid module id "MY_NAMESPACE__My_Custom_Permission" for type "customPermission". Explicit use of namespace "MY_NAMESPACE" in file "myLwcComponent.js" is prohibited. Use default namespace "c" instead.

e.g.

For import hasCustomPermission from '@salesforce/customPermission/c__My_Custom_Permission';

Error: Invalid reference c__My_Custom_Permission of type customPermission in file myLwcComponent.js

4
  • Did you try with salesforce/customPermission/namespace__PermissionName Ref - releasenotes.docs.salesforce.com/en-us/summer20/release-notes/…
    – Ysr Shk
    Aug 17, 2020 at 5:17
  • 3
    @YsrShk yes, but because it's referencing the namespace of the current org (not the namespace of an installed package), it throws an error. I've updated the question with the errors.
    – Nick C
    Aug 17, 2020 at 5:31
  • 1
    I think it's one of those scenarios where they released the new feature and never tested putting it in managed package. Like they did LWC local previews and a bunch of other things. I'd say raise a support case and wait... for a few months :(
    – zaitsman
    Aug 17, 2020 at 5:33
  • @NickCook I doubt this would work, but no harm in trying it out. Do you see the same behavior when the namespace and custom permission name doesn't include 'underscore'? Although, 'underscore' is valid in the naming rules for LWC, just want to check there is no bug related to that causing this error [I'm suggesting this because the release notes from SF specifically mentions about custom permission from managed package, but the examples given don't include any 'underscore']. Also, no case-sensitivity mismatch in the code?
    – arut
    Aug 17, 2020 at 5:52

2 Answers 2

1

This is one of those cases where you're meant to use a Scratch Org. Register your namespace with your dev hub, and use that for development instead of a Developer Edition org. Use the c__permission notation, and everything should work just fine. I do find it frustrating that the code isn't portable between namespaced and non-namespaced orgs, although I'm not entirely surprised, as there's been numerous issues with namespaces all throughout Salesforce's history. Using Scratch Orgs is the main way to avoid any and all namespace development issues, since you never have to deal with a non-namespaced Development Edition ever again.

1
  • The c__permission doesn't work in non-namespaced scratch orgs. You get an error like the following when trying to deploy: Invalid reference c__Permission of type customPermission in file component.js Sep 15, 2020 at 19:14
0

The way you created the permission is under Setup Custom Permission or Permission Set? If you have done it in Permission set, you will get an error, if you do it in Custom permission, you are doing it right.

To check if a user is assigned to a specific permission set, you first create a Custom permission and name if. After that, go to the permission set you have created and go to:

Custom Permissions

Permissions to access custom processes and apps

Assigning the Custom Permission you have created to the permission set. Then, you can assign the users.

Now, calling this, with the user assigned, you will get true in the console:

    import { LightningElement } from 'lwc';
import MyUserPermission from '@salesforce/customPermission/My_User_Permission';
import hasRunReports from '@salesforce/userPermission/RunReports';

export default class TestMyUserPermission extends LightningElement 
{
    get isRunReport() {
        return hasRunReports;
    }
    get isMyUserPermissionEnabled() {
        return MyUserPermission;
    }


    connectedCallback() {
        console.log('isMyUserPermissionEnabled: '+ this.isMyUserPermissionEnabled);
        console.log('isRunReport: '+ this.isRunReport);
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .