You need to sign in to do that
Don't have an account?
SFDC n12
getting the currently logged in user 's profile from user object
Hi,
I want to get the currently logged in user profile from user object , Here is the code below
Utility class :
I am having a utility class which contains the logic for query to fetch the currently logged in user and which i am calling the same method in my main class
public static User getUserInfo(String profileId){
// String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
User usr = [Select Id,Name,UserType,Employee_HR_ID__c,EmployeeNumber,profile.Name from User Where profile.Name =:profileName AND isActive=true];
//User usr = [Select Id,Name,UserType,Employee_HR_ID__c,EmployeeNumber,profile.Name,Profile.UserLicense.Name from User Where Profile.UserLicense.Name = 'Salesforce' AND isActive=true LIMIT 1];
return usr;
}
Main class where utility mehod is called :
public PageReference generateReport() {
PageReference pg = null;
//String userId = UserInfo.getUserId();
String profileId =userinfo.getProfileId();
User usr = AF_DealerCRM_Utility.getUserInfo(profileId);
System.debug (' ProfileName: ' + usr);
}
Kindly let me know wat changes do i need to make in my code
Thanks in advance
I want to get the currently logged in user profile from user object , Here is the code below
Utility class :
I am having a utility class which contains the logic for query to fetch the currently logged in user and which i am calling the same method in my main class
public static User getUserInfo(String profileId){
// String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
User usr = [Select Id,Name,UserType,Employee_HR_ID__c,EmployeeNumber,profile.Name from User Where profile.Name =:profileName AND isActive=true];
//User usr = [Select Id,Name,UserType,Employee_HR_ID__c,EmployeeNumber,profile.Name,Profile.UserLicense.Name from User Where Profile.UserLicense.Name = 'Salesforce' AND isActive=true LIMIT 1];
return usr;
}
Main class where utility mehod is called :
public PageReference generateReport() {
PageReference pg = null;
//String userId = UserInfo.getUserId();
String profileId =userinfo.getProfileId();
User usr = AF_DealerCRM_Utility.getUserInfo(profileId);
System.debug (' ProfileName: ' + usr);
}
Kindly let me know wat changes do i need to make in my code
Thanks in advance
You have to do like this,
public googlePageController(){
PageReference pg = null;
String usr = AF_DealerCRM_Utility.getUserInfo();
System.debug (' ProfileName: ' + usr);
}
public string getUserInfo(){
String usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()].Profile.Name;
return usrProfileName ;
}
(OR)
public googlePageController(){
PageReference pg = null;
User usr = AF_DealerCRM_Utility.getUserInfo();
System.debug (' ProfileName: ' + usr.Profile.Name);
}
public User getUserInfo(){
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
return usrProfileName ;
}
please select this as solution if you got your answer.
Thanks,
Praveen Murugesan
All Answers
http://salesforce.stackexchange.com/questions/18108/how-to-get-the-current-user-profile-in-apex-class
You have to do like this,
public googlePageController(){
PageReference pg = null;
String usr = AF_DealerCRM_Utility.getUserInfo();
System.debug (' ProfileName: ' + usr);
}
public string getUserInfo(){
String usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()].Profile.Name;
return usrProfileName ;
}
(OR)
public googlePageController(){
PageReference pg = null;
User usr = AF_DealerCRM_Utility.getUserInfo();
System.debug (' ProfileName: ' + usr.Profile.Name);
}
public User getUserInfo(){
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
return usrProfileName ;
}
please select this as solution if you got your answer.
Thanks,
Praveen Murugesan
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
or
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getProfileId()];
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
We are comparing with user id only.
Else.
Do like this
User usrProfileName = [select u.Profile.Name from User u where u.ProfileId = :Userinfo.getProfileId()];
User usrProfileName = [select u.Profile.Name from User u where u.ProfileId = :Userinfo.getProfileId() LIMIT 1];
Since without the limit its retriving all user records with this profile as a result of which i am getting "List has more than 1 rows assignment to s object " error
Since you can assign only one profile to a user you need to put limit 1.
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
I think this is not the best way.
User usrProfileName = [select u.Profile.Name from User u where u.ProfileId = :Userinfo.getProfileId() LIMIT 1];
Thanks.
User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()];
only this query corresponds to user profile right
User usrProfileName = [select u.Profile.Name from User u where u.ProfileId = :Userinfo.getProfileId() LIMIT 1];
So this query "User usrProfileName = [select u.Profile.Name from User u where u.id = :Userinfo.getUserId()]; " only one row always. We are getting profile.name from currently logged in user's user record.
But if we use the output will be from first record. It may or may not be Currently logged in User's record
User usrProfileName = [select u.Profile.Name from User u where u.ProfileId = :Userinfo.getProfileId() LIMIT 1];
For Eg:
In our org there are 100 system Administrator working if we use the second query. Then from 100 records it will query either 99th or 10th or 1st... so it may get the profile name from other user.