How to execute Apex code in controller only for specific profile, like System Administrator? So that when other users visit Visualforce page, the code block would not be skipped?


Use UserInfo Class

This class let you check profile and based on this execute it or not.


private static final Id SYS_ADMIN_PROFILE_ID = 
         [select ProfileId 
             from User 
             where Profile.Name = 'System Administrator'].ProfileId;

If(UserInfo.getProfileId() == SYS_ADMIN_PROFILE_ID ){
/** your code **/

SYS_ADMIN_PROFILE_ID is variable that contains Id of System Administrator profile, you can get it by SOQL and keep in one of class as static

  • Better than mine! – rael_kid Feb 26 '14 at 14:38
  • Hardcoding the Id of the Sys Admin profile in a class is a bad idea as the Id's will not be the same in every org. If you're using this approach then make sure that you have a mechanism for changing what the SYS_ADMIN_PROFILE_ID is set to. – BarCotter Feb 26 '14 at 14:57
  • I understand your point but please read again my post (especialy "you can get it by SOQL and keep in one of class as static" part :)) – Artur Kępczyński Feb 26 '14 at 14:58
  • Sorry @Birthus, I read it as the SOQL to get the Id as being a manual step, where the result would be hardcoded as a magic number in the code. Ive updated your answer to make the code clearer. – BarCotter Feb 26 '14 at 15:03
  • I would do a LIMIT 1 in the SOQL as there could be more than 1 users with 'System Administrator' profile. – Jarvis Mar 12 '16 at 9:02

I believe you can also opt for hierarchy custom setting option for your scenario as it will provide the ability to perform different action based on logged in user/profile on apex classes. For more details ,follow the below link

Regards Anchal Garg


You can query the user object to find out which profile the current user has:

List<User> u = [SELECT ProfileId FROM User WHERE Id = :UserInfo.getUserId()];
if(u[0].ProfileId == 'some profile id'){
     //your code

Then you can either specify the id in a Custom setting, or hard-coded, if the case allows that.


You could use the code below. You would need to create a Set called setOfAllowedProfileNames that contains all the ProfileNames that you'd like the code to run for. The Set could be built from a CSV custom setting, or Custom Label.

The benefit of this approach is that you are specifying the profile names rather than than the profile Ids, which will vary in different orgs.

private Set<String> setOfAllowedProfileNames {
    get {
        if (setOfAllowedProfileNames == null) {
            // Build the set...
    return setOfAllowedProfileNames;

private String currentUsersProfileName {
    get {
        if (currentUsersProfileName == null) {

            currentUsersProfileName = [
                select Profile.Name 
                from User 
                where Id = :UserInfo.getUserId()].Profile.Name;
        return currentUsersProfileName;

private Boolean isUserInAllowedProfiles {
    get {
         if (isUserInAllowedProfiles == null) {
              isUserInAllowedProfiles = setOfAllowedProfileNames.contains(currentUsersProfileName);
         return isUserInAllowedProfiles;

...where Profile.Name = 'System Administrator'...

does not work in a multi language environment!

Until yesterday I was using

//Get the organisation creation date    
Datetime orgCreate = [Select o.CreatedDate From Organization o].CreatedDate; 
//Add a minute or two for the creation process
orgCreate = orgCreate.addminutes(1);
//Use that date/time to look ONLY for standard profiles with PermissionsCustomizeApplication  
Profile adminProfile = [Select id, name, CreatedDate 
                            From Profile 
                            Where CreatedDate < :orgCreate 
                            AND PermissionsCustomizeApplication = true Limit 1];

That way I got the one standard profile with PermissionsCustomizeApplication, but now I learned, that Service Cloud has that permission as well

  • have you come up with a better solution since then?) – bananka Sep 4 '15 at 12:58

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.