Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

Use UserInfo Class

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

Try:

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

share|improve this answer
    
Better than mine! –  Lex Feb 26 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 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 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 at 15:03
    
No problem, and thanks for adding query! :) –  Artur Kępczyński Feb 26 at 15:03
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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;
    }
    set;
}

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

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

private Boolean isUserInAllowedProfiles {
    get {
         if (isUserInAllowedProfiles == null) {
              isUserInAllowedProfiles = setOfAllowedProfileNames.contains(currentUsersProfileName);
         }
         return isUserInAllowedProfiles;
    }
    set;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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