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

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

Sample Salesforce Custom Permission:

Sample Code:

Boolean accessBool = 
    FeatureManagement.checkPermission(
        'Create_Account'
    ); 
System.debug(
    'accessBool is ' + 
    accessBool
);

( Or )

Boolean accessBool;  
Set < Id > permissionSetIds = new Set < Id >();  
List < User > userList = new List < User >();  
for ( SetupEntityAccess access : [ SELECT ParentId FROM SetupEntityAccess   
                                   WHERE SetupEntityId IN ( SELECT Id  
                                                            FROM CustomPermission  
                                                            WHERE DeveloperName = 'Create_Account' ) 
] )   
    permissionSetIds.add(access.ParentId); 
     
userList = [ SELECT Username FROM User WHERE Id IN (  
        SELECT AssigneeId FROM PermissionSetAssignment 
        WHERE PermissionSetId IN :permissionSetIds AND AssigneeId = UserInfo.getUserId() ) ]; 
accessBool = userList.isEmpty() ? false : true; 

Leave a Reply