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
SFDC n12SFDC 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
Best Answer chosen by SFDC n12
praveen murugesanpraveen murugesan
Hi Bharath,

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

Ramu_SFDCRamu_SFDC
The below post might help

http://salesforce.stackexchange.com/questions/18108/how-to-get-the-current-user-profile-in-apex-class
praveen murugesanpraveen murugesan
Hi Bharath,

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
This was selected as the best answer
SFDC n12SFDC n12
Thanks dude , it worked... :)
SFDC n12SFDC n12
but in this query is it

 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()];
praveen murugesanpraveen murugesan
The query should be like this.

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()];
SFDC n12SFDC n12
i should use limit in my query right

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
praveen murugesanpraveen murugesan
Yes. Ofcourse.

Since you can assign only one profile to a user you need  to put limit 1.
praveen murugesanpraveen murugesan
This will be the correct query,

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.


SFDC n12SFDC n12
but the below query doesnt correspond to user profile right,

 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];
praveen murugesanpraveen murugesan
No not like that.  Our objective is to getting the currently logged in user 's profile name from user object.

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.