How to get the current User Profile in Apex Class?

  • What is the right way to get the current users profile in an apex class?

    When I tried to print the following in visualforce page, i am able to get the profile name

    <apex:outputText value="{!$Profile.Name}"/> 

    But when i tried to print in the Apex class using System.debug(Profile.Name), I couldn't.

  • Id profileId=userinfo.getProfileId();
    String profileName=[Select Id,Name from Profile where Id=:profileId].Name;

    Refer the above code

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM