How to check Custom Permission assigned to the current user's profile using Apex in Salesforce?

Sample Code:


  1. Boolean accessBool;  
  2. Set < Id > permissionSetIds = new Set < Id >();  
  3. List < User > userList = new List < User >();  
  4. for ( SetupEntityAccess access : [ SELECT ParentId FROM SetupEntityAccess   
  5.                                    WHERE SetupEntityId IN ( SELECT Id   
  6.                                                             FROM CustomPermission   
  7.                                                             WHERE DeveloperName = 'Create_Account' )  
  8. ] )   
  9.     permissionSetIds.add(access.ParentId);  
  10.       
  11. userList = [ SELECT Username FROM User WHERE Id IN (  
  12.         SELECT AssigneeId FROM PermissionSetAssignment  
  13.         WHERE PermissionSetId IN :permissionSetIds AND AssigneeId = UserInfo.getUserId() ) ];  
  14. accessBool = userList.isEmpty() ? false : true;  

( Or )


  1. Boolean accessBool = FeatureManagement.checkPermission('Create_Account');  

No comments:

Post a Comment