Sign up ×
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

5 Answers 5

up vote 6 down vote accepted

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

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

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

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

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;
share|improve this answer

...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

share|improve this answer
have you come up with a better solution since then?) – bananka Sep 4 at 12:58

Your Answer


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.