How can I Efficiently Check if the Running User has a Custom Permission?
As of the Winter 18 Release, we can now use the FeatureManagement
class for this requirement. The above method can be removed entirely and I can simply call:
Boolean hasPermission = FeatureManagement.checkPermission('CanPerformSomeOperation');
Thanks to @sfdcfox for pointing this one out.
Adrian Larson's answer of using FeatureManagement
's static Boolean checkPermission(String customPermissionDeveloperName)
will Efficiently Check if the Running User has a Custom Permission.
Testing Apex when using FeatureManagement
's static Boolean checkPermission(String customPermissionDeveloperName)
isn't so simple. Often, using Custom Permissions will grant a User something private or special, and I want to make sure my Unit Test is doing what I think it's doing when a User both has and doesn't have the Custom Permission(s) in question. So, I made a github repo, TestCustomPermissions, to automate adding Custom Permissions. See README.md for documentation and example.