function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Divya GoelDivya Goel 

How to get profile name without query

Is there any way to get the profile name of logged in user without querying the user object.


I know the following solution:


String usrProfileName = [select u.Profile.Name from User u where = :Userinfo.getUserId()].Profile.Name;


Instead of this I want to save 1 query so if there is any method to get the profile name in apex without firing the query please let me know.



Richie DRichie D

I'm don't think there is - I've never come across one. (You can get the profileId from the UserInfo by the way - UserInfo.getProfileId() which tidies your code up a bit).


Why do you want to save 1 query? Is there somewhere else you could get this saving?





As a suggestion, you can sometimes get away with getting related information in another query. For example, if you were doing an account listing, you might do something like this:


List<account> useraccounts = [select id,name, from account where ownerid = :userinfo.getuserid()];


This would give you the account information you were looking for, while also grabbing the user's profile name. Given that there is no apparent way to get the name without a query, if you can leverage another query to get the data, you can save yourself an SOQL call.


If you're bumping up against the upper limits of the governor, restructuring or combining queries might be a solution. I had a project like that where I was ultimately hitting over 30 queries per trigger, and I reduced it to 14 by restructuring the code to take advantage of similar queries.


However, that being said, if you're only at 3 or 4 queries, don't optimize unless you see a major performance hit. Saving one query, especially one that's only one row, isn't the best use of your time. Focus on larger queries where you could get better performance, and if you still need that extra query, then work in a solution to grab that information while you're working on related data.


I'm trying to do something similar. I'm trying to write an Apex trigger that fires each time an Account is updated. Whenever the Owner changes, I want another field to be updated based on the new Owner's profile. But I need to do this without running any SOQL queries because otherwise, Salesforce won't let me do a mass transfer of Account ownerships. I can't do a formula because for whatever reason, it won't let me access the Account Owner from there. I can write a bunch of workflows, but I have to write one for each user in the system. There must be a more elegant way of doing this.


Any ideas out there?


I tried something like this:[i].Owner.Profile.Name.compareTo("Sales User"), where[i] is the Account after it has been updated. But I keep getting these NULL reference exceptions. So is there a way for me to access the Account Owner's Profile Name from the trigger without running a SOQL query?


Andrew, did you ever figure this out? I'm working on the same thing.

Thys MichelsThys Michels

Andrew did you figure this out?I am also looking for a solution for this.




To display Profile Name in VF page you can do like this.





Hi Divya,
You can use following method to by pass for any specific user by saving his/her User name in the Label and add check as follows :

Check the following link  to get it through script :

Hope this helps,

Dhananjay Misal 8Dhananjay Misal 8
Hi Divya,

    Best answer to your query is using the following syntax :

Devin lensanDevin lensan
The UserInfo class provides a number of methods to get information about the current user, such as getUserEmail(), getFirstName(), getLastName(), getUserId(), etc. However, there is no method to get the profile name.
So, the only way to get the profile name in Apex is by querying the User object as you have shown in your code.
itunes (
Atosi MalakarAtosi Malakar
You can create o ne formula field in user object and store the profile name in that field. 
This field can be used in query.