How to get the current User Profile in Apex Class?

Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
system.debug('ProfileName'+profileName);

Refer the above code


I know I'm coming late to this party, but I solved this for my particular case by doing the following;

  1. define a formula field (named, say, "CurrentUserProfile) on an object that has this code:
    $UserProfile.Name

  2. In your trigger (or class), you can just refer to the variable from the object;

     for(Task t :triggerNew) {
     // ...
     System.debug('Current User Profile: ' + t.CurrentuserProfile__c);
     // ...
     }
    

Hope this helps.... You can do the same for the Role name too.

Tags:

Apex