There is a method FeatureManagement.checkPermission to check efficiently if a user has a custom permission.

How can we check efficiently if a user has a standard permission instead?

For example, when I try to find if a user has Manage Profiles and Permission Sets permission and when I run the following code under system administrator user

System.debug(LoggingLevel.ERROR, '@@@ v: ' + FeatureManagement.checkPermission('ManageProfilesPermissionsets')  );

I receive

@@@ v: false

results despite system administrator has Manage Profiles and Permission Sets permission.


So the shortest code is the following:

![SELECT Id FROM PermissionSet where PermissionsManageProfilesPermissionsets = true AND Id IN (select PermissionSetId from PermissionSetAssignment where AssigneeId =:UserInfo.getUserId())].isEmpty();

However, if there is a need to check for two or more different standard permission, it is not efficient to perform multiple SOQL query and in such case it is still needed to loop through the query results like following:

Boolean result = false;
Boolean manageProfiles = false;
Boolean customizeApp = false;
for (PermissionSet ps: [SELECT PermissionsManageProfilesPermissionsets, PermissionsCustomizeApplication FROM PermissionSet where ID IN (select PermissionSetId from PermissionSetAssignment where AssigneeId =:UserInfo.getUserId())]) {
    if (ps.PermissionsManageProfilesPermissionsets) manageProfiles = true;
    if (ps.PermissionsCustomizeApplication ) customizeApp = true;
    if (manageProfiles && customizeApp) break;
result = manageProfiles && customizeApp;
System.debug(LoggingLevel.ERROR, '@@@ v: ' + result );

which yields

@@@ v: true
    List<PermissionSet> userPermissionsList = [SELECT PermissionsManageProfilesPermissionsets, PermissionsCustomizeApplication 
    FROM PermissionSet 
    WHERE(PermissionsManageProfilesPermissionsets = true OR PermissionsCustomizeApplication = true) 
    AND Id IN (select PermissionSetId from PermissionSetAssignment where AssigneeId =:UserInfo.getUserId())];
    String userPermissions= String.join(userPermissionsList , '-');
    Boolean userHasPermission = (userPermissions.contains('PermissionsManageProfilesPermissionsets:true') 
&& userPermissions.contains('PermissionsCustomizeApplication:true'));
    System.debug(userHasPermission); //true

would be shorter way to write it.

  • in my actual case I need to check two permissions, namely ManageProfilesPermissions and CustomizeApplication, and these two permissions might be assigned by two permission sets so I will still have to query both and loop through the results – Patlatus Apr 15 '19 at 19:59
  • Well, it is possible without for but not necessarily better option. I updated my answer. – tugce Apr 21 '19 at 11:45
  • The idea of concatenating SObject records and then checking if the concatenated string contains required permission is interesting. However, I find ? true : false; to be extra in your code snippet. – Patlatus Apr 23 '19 at 10:28
  • Yup, it is extra. Thanks for pointing it out. Concatenating & using contains is not something I did before but for me it was only way to remove for loop. – tugce Apr 23 '19 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.