How to get the Role name and Profile Name from User Object using SOQL

We can able to get the Role Name and Profile Name details from User Standard Object

SOQL Query:
SELECT Profile.Name, UserRole.Name FROM User

below is the code to retrive the User Role Name and Profile Name based on the logged in user in Apex Class

Apex Class:

Id userId = userinfo.getUserId();
User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User where Id=:userId ];
system.debug('Profile Name:' + userDetails.Profile.Name);
system.debug('Role Name:' + userDetails.UserRole.Name););

Here:
User is a Standard Object
UserInfo Class – we can retried logged in user

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *

*