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 u.id = :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.

 

Thanks,

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?

 

 

  

sfdcfoxsfdcfox

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,owner.profile.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.

Andrew1222Andrew1222

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?

Andrew1222Andrew1222

I tried something like this:

 

Trigger.new[i].Owner.Profile.Name.compareTo("Sales User"), where Trigger.new[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?

Scott-3823Scott-3823

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.

suresh.csksuresh.csk

Hi.

 

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

{!$Profile.Name}

 

cheers

suresh

PrashanthNeoPrashanthNeo
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 :
!UserInfo.getName().contains(Label.Client_User))

Else 
Check the following link  to get it through script :
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_getuserinfo.htm

Hope this helps,

Thanks,
Prashanth
Dhananjay Misal 8Dhananjay Misal 8
Hi Divya,

    Best answer to your query is using the following syntax :

              {!$profile.name}
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 (https://myappforpc.com/app/915061235/itunes-store)
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.