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
Rija SanaRija Sana 

Get profile name of any user by name

Im trying to creat a trigger that monitors activities by profiles. How would I find profile name of any user by using the users' name?
Best Answer chosen by Rija Sana
Andrew GAndrew G
A little confused to why a trigger for any user but:
 
SELECT user.id, user.UserName, user.profile.name FROM user
That should point you in the right direction
 

All Answers

Andrew GAndrew G
Two options:
In your trigger do a SOQL query:

List<Profile> profiles = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1];
String MyProflieName = profiles[0].Name;

Alternate, if you don't want to use SOQL query

Create a formula field such as 'CurrentUserProfile' on the object with formula:  $UserProfile.Name
In the trigger, refer to the field:
System.debug('Current User Profile: ' + record.CurrentUserProfile__c);

Regards 
Andrew

 
Rija SanaRija Sana
Actually, i'm looking for profile name of any user by their id or username not just the current user. Activities are added y multiple systems and i cant reply on current user to be of the right profile.
 
Andrew GAndrew G
A little confused to why a trigger for any user but:
 
SELECT user.id, user.UserName, user.profile.name FROM user
That should point you in the right direction
 
This was selected as the best answer