+ Start a Discussion
neoocmwtfneoocmwtf 

Detect if user is System Administrator

Hi there everyone.

 

Does anyone know of a way of detecting if the user is a system administrator?

I tried by comparing the user's profile name but I think this changes with the language and need multilanguage support.

 

Any ideas?

Thanks!

WilmerWilmer
Try to compare directly the user profile Id.
srisomsrisom

You could create a custom label (setup->create->custom label) and store the sys admin profile id in a label.  You could then compare the user profile id with the label.

 

Id sysProfileId = Label.sys_admin_prof_id;

if (sysProfileId == UserInfo.getProfileId()) {

....

 

Whenever you deploy your code anywhere you will need a one off check that your ids are all correct for the target environment.  Benefit is that you don't issue a query - nice especially in triggers where the governor limits are low.

NOTSOFASTNOTSOFAST

If you are willing to accept that a user with "modify all data" permission is a system admin then this might help:

 

    public static boolean isSysAdmin(String thisUserId) {
        boolean isAdmin = false;
        User thisUser;
        try {
            thisUser = [SELECT Profile.PermissionsModifyAllData
                FROM User WHERE u.Id =: thisUserId];
            isAdmin = thisUser.Profile.PermissionsModifyAllData;
        } catch(Exception exc) {
            //do nothing
        }
        return isAdmin;
    }

Avinash Jha 6Avinash Jha 6
Use the follwoing code to get profile name 
String userProfileName = [select Name from profile where id =: userinfo.getProfileId()].Name;

and you can simply do a string comparison to know if the profile is system admin
if(userProfileName == 'System Administrator'){
        do stuff
    }else{
        do stuff
}