I want to check Profile Name of Users inside a for loop so I need their profile names without querying in apex is there any workaround this.


If you don't want to burn a SOQL Statement and you're working with an SObject, you can use this approach:

  1. Formula field (named, say, "CurrentUserProfile) on an object that has this code:


  2. In your trigger, you can refer to the variable;

    for(Object__c obj :triggerNew) { System.debug('Current User Profile: ' + obj.CurrentuserProfile__c); }

| improve this answer | |
  • 1
    I am using this method currently but will like to see if there are any other solutions to the problem as client may not agree with a additional field. – Ishan Apr 17 '17 at 9:07
  • I ended up following this method as it is more reliable and better than using system cache. – Ishan Apr 21 '17 at 6:19
  • I think this is the optimum method. – Adamo Apr 22 '17 at 8:33

Another alternate way would be to use Platform Cache. Using this you can store the details of profile using Map<Id,Profile> which will map profileId with their Profile. And whenever you want to get the name of profile for any user then simply use map.get(UserInfo.getProfileId()) method to get the profile name.

Using platform cache what you can do is query all the profiles at once and then simply use it any Apex class. There is another alternative to use custom settings but that's not a feasible approach.

Sample Example:

Map<Id,Profile> profileMap = [SELECT Id,Name FROM Profile LIMIT 100];
Cache.Org.put('light002.partition1.profileList', profileMap);
if (Cache.Org.contains('light002.partition1.profileMap')) {
    String profileName = profileMap.get(UserInfo.getProfileId()).Name;
| improve this answer | |
  • I haven't used this feature so I would like to know if this will work during execution of batch jobs as there will be multiple users how can I make sure that correct profile name coincides with correct user. – Ishan Apr 17 '17 at 9:39
  • @Ishan I've mentioned a link of platform cache in my answer. You can go through it. Let me give you highlight of my answer. In the code sample, I've created a Map<Id,Profile>. This will have all profiles mapped with their id's. You just need to get profile by passing profileId of current user using map.get(UserInfo.getProfileId()). Platform cache is a great feature provided by salesforce to store the data like we do using session storage, browser cache and cookies. However in this you can retrieve cache data in your apex class. – Rohit Mourya Apr 17 '17 at 9:44

Once you have a session you can call getUserInfo(). In the getUserInfoResult() there is the profileID in the response.

Now that you have the profileId you can send a SOQL query off to Profile to get the Name field.

select id, name from profile where Id = '00e30000000hDW1AAM'

When performing a SOQL query against Opportunity you can get the last modified user name with LastModifiedBy.Name. E.g.

Select Id, Name, LastModifiedBy.Id, LastModifiedById, LastModifiedBy.Name From Opportunity.

Unless you are already sending a query to something like the User object you won't be able to get the Profile name without an additional query. You can use the userInfo from the LoginResult to save one API query. However, at some point you are going to need a query to go from the ProfileId to the Profile.Name

| improve this answer | |
  • Actually, I am inserting user in bulk and assigning some custom Id based on profile so I don't want to do a query. – Ishan Apr 17 '17 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.