1

has anyone found a way to check if the current user is a sysadmin without consuming a query? Seems like something that should be easily determined using Describe but haven't seen a solution to this. Used frequently in my org, and would be great to get rid of a query. Thanks.

3

No, you can't. For this purpose, we created a CurrentUserInfo class that has a number of values automatically lazy-loaded from the database on first use, and cached later uses. A simplified version of our code might look like this:

public class CurrentUserInfo {
  public static Boolean isAdmin = false;
  static {
    for(PermissionSetAssignment psa: [SELECT PermissionSet.PermissionsModifyAllData FROM PermissionSetAssignment WHERE AssigneeId = :UserInfo.getUserId()]) {
      isAdmin |= psa.PermissionSet.PermissionsModifyAllData;
    }
  }
}

You can add more logic to this to cache as much information as you'd like about the current user. Yes, you still get hit with a query, but only once per transaction, no matter how many times you use it. Actual use of the class works as you might expect:

if(CurrentUserInfo.isAdmin) { // they are an administrator...
1
  • Also include AND PermissionsCustomizeApplication = TRUE check to ensure its Sys Admin Jun 5 '19 at 14:26

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.