How to Get Current User Info Using Apex in Salesforce

As a Salesforce Developer, I was required to display the details of the currently logged-in user, such as their name, email, and profile, in Salesforce. This feature might be used in a dashboard where users can see their account information. To achieve this requirement, I developed an Apex class that retrieves current user details.

Here, I will explain current users in Salesforce and show how to get current user info using Apex in Salesforce. Additionally, I will also explain other user methods so that we can perform operations on user records, like deactivating users, resetting user passwords, etc.

Current User in Salesforce

In Salesforce, the current user is actively logged in, interacts with the Salesforce UI, and performs operations like creating records, managing data, automating processes, etc. When we create a new user in Salesforce, we have to fill out some mandatory fields, such as name, alias, email, username, profile, time zone, and other information.

Salesforce provides developers and administrators with the feature to fetch/retrieve information about users using the Apex class, VF pages, Aura and LWC components, and the global variable($User).

There are two ways to fetch user details in Salesforce Apex.

  • UserInfo Class: Using the UserInfo class in Apex, we can access information about the current user, such as their ID, profile, role, and other user details. This class is useful when you need to retrieve data about actively logged-in users in Salesforce.
  • SOQL: We can also use SOQL to retrieve user or current user details in Apex and add conditions for the records we want to retrieve.

Get Current User Info Using UserInfo Class in Salesforce Apex

In the steps below, I will explain how to retrieve Salesforce users and current user information using the UserInfo class.

Create Apex Class & Method() in Salesforce

First, we will create a class and a function to display the user details that we will fetch from the UserInfo class methods.

To create the Apex class, click the settings icon on the Salesforce Home Page and the Developer Console. Then, create an Apex Class and enter the class name. The class will be created with the class name that you entered.

Then, add a method to the class so that we can display the UserInfo class method to retrieve the current user’s details. Here, I created the DisplayUserInfo method, which we can execute to display the user details.

public class GetUserInfo {    
    Public void DisplayUserInfo(){        
        //UserInfo class Methods to retrieve Current User details
    }
}

1. GetFirstName() and GetLastName()

  • GetFirstName(): This is the UserInfo class method that is used to retrieve the currently logged user’s first name.
  • GetLastName(): This is the UserInfo class method for retrieving the currently logged user’s last name.

First, we need to declare a String variable to store the first and last names. Then, we use the UserInfo class directly to retrieve the data. Here we stored the first and last names in the fname and lname string variables, respectively.

Then, using the concatenation+‘ operator, I displayed both names in one string. We also have the GetName() method to return the current user’s full name.

String fname = UserInfo.GetFirstName();
String lname = UserInfo.GetLastName();

System.debug('Current User First Name is: ' +fname);
System.debug('Current User Last Name is: ' +lname);

System.debug('User Full Name is: ' +fname + ' ' +lname);
How to Get Current User Info Using Apex in Salesforce

2. GetUserId()

The GetUserId() method is used to fetch the ID of the currently logged-in user. To get and store the current user ID, we need to create a variable with the data type ID.

ID userid = UserInfo.GetUserId();
System.debug('Current User ID: '+userid);
Get Current User ID in Salesforce Apex

3. GetUserEmail()

We can retrieve the currently logged-in user’s email address using the GetUserEmail() method. To store the email, we created a string variable.

String UserEmail = UserInfo.GetUserEmail();
System.debug('Current User Email Address is: '+UserEmail);
Get Current User Email in Salesforce

4. GetUserName()

The GetUserName() method of the UserInfo class fetches the logged-in user’s user name.

String UserName = UserInfo.GetUserName();
System.debug('Current logged-in User Name: '+UserName);
Get Current UserName in Salesforce Apex

5. GetProfileId()

Using GetProfileId(), we can fetch the profile ID assigned to the currently logged-in user.

String ProfileId = UserInfo.GetProfileId();
System.debug('Current User ProfileID: '+ProfileId);
Current User ProfileID in Salesforce Apex

6. GetTimeZone()

The GetTimeZone() method of the UserInfo class returns the current user’s local time zone.

We fetched the time zone using the GetTimeZone() method and needed the TimeZone data type. Then, we also fetched local time, which means GMT time. For that, we don’t have any UserInfo class method to fetch local GMT time. Still, we can use the getDisplayName() method under the variable where we stored the current user time zone, which means ‘UserTime.getDisplayName()‘, which will return the current user’s local GMT time.

TimeZone UserTime = UserInfo.GetTimeZone();
System.debug('Current User Local Time Zone: ' +UserTime);
System.debug('Local Time : ' +UserTime.getDisplayName() );
Current User Time Zone in Salesforce Apex

7. GetDefaultCurrency()

The GetDefaultCurrency() method of the UserInfo class fetches the logged-in user’s default currency type, such as INR, Dollar, Pound, etc. To store the currency type here, I created a string data type variable.

String usercurrency = UserInfo.GetDefaultCurrency();
System.debug('Current User Currency: ' +usercurrency);
Get Default Currency in Salesforce Apex

These are some essential methods to retrieve the current user details using the UserInfo class in Salesforce.

Get Details of Currently Logged-in Users in Salesforce Apex

Now, I will explain how we can retrieve the details of currently logged-in users, such as their name, UserId, and profile, for assigned users using SOQL in Salesforce Apex.

Here, I created a class with a method and then used the UserInfo class with the getUserId() method, which returns the currently logged-in user ID. I passed that ID into the SOQL query, which retrieves the user details.

public class GetUserDetails{
    
    public void userdetails(){
         Id userId = UserInfo.getUserId();
        User u= [SELECT Name, Id, Email, Profile.Name FROM User WHERE Id = :userId];
        system.debug('Current User Details: ' +u);
    }
}
Get Current User Details in Salesforce Apex

In this way, we can get whatever details we want of currently logged-in users we can retrieve using the UserInfo class method and SOQL query in Salesforce Apex.

Get Profile and License of User in Salesforce Apex

We can use the UserInfo class to get the current user’s assigned license and profile, and another method is to provide the user ID in the SOQL query to get the profile and license.

Get the Current User’s Assigned License and Profile

To get the currently logged-in user profile and license, first, we need to create a variable with the data type ID. We will store the current user ID in that variable by using the UserInfo class and the getUserID method().

Then, we will write an SOQL query to retrieve the profile name and license name from the currently logged-in user ID that we stored in a variable.

Now, when you retrieve the profile name and user license name directly, you will get the ID of the profile and license. Because the profile and license names are looked up with the User, to get the values of the fields, we need to enter Profile.Name and Profile.UserLicense.Name, and it will get IDs.

Those IDs will be stored in the userObj variable. Now, to get the profile and license name, we need to enter ‘userObj.Profile.Name‘ and ‘userObj.Profile.UserLicense.Name‘. After that, execute the class.

public class UserLicense {
    
    public void GetUserLicenseAndProfile(){
        
        Id userID = UserInfo.getUserId();
        User userObj = [Select id, Profile.Name, Profile.UserLicense.Name from User where Id=: 
                                   userID];
        System.debug('Profile Name:  ' +userObj.Profile.Name + ', License: ' 
                                   +userObj.Profile.UserLicense.Name);           
    }
}
Get Current User Profile Name and License in Salesforce Apex

In this way, we can get the current user profile name and license and assign them using the Salesforce Apex class.

Get Profile and License Assigned to user by User ID

We have seen how to get the profile and license name of the currently logged-in user. Now, when we want to get another user’s profile and license name, we can pass the ID in the SOQL statement. Here, I stored the user ID in a variable with ID as the data type and passed that variable into the query.

public void getLicensebyID() {
        
        ID userID = '005J3000000h8DlIAI';
        User userObj = [Select id, Profile.Name, Profile.UserLicense.Name from User where Id=: 
                                   userID];
        System.debug('Profile Name:  ' +userObj.Profile.Name + ', License: ' 
                                   +userObj.Profile.UserLicense.Name);
    }
Get User Profile Name and License by User ID in Salesforce Apex

In this way, we can get the current user profile name and license and assign it to them using the Salesforce Apex class.

Retrieve Details of All Active Users Using Salesforce Apex

In this example, I will show you how to fetch all active user details using SOQL in Salesforce Apex.

Here, I created a class with one method to display the user details we will retrieve using SOQL.

  • List<>: When we have multiple records to display, we need to create a List collection data type to store those records.
  • SOQL: Enter the query to fetch user details with the WHERE clause to get only all active users.
  • For Loop: In a for loop, we need to create a User object instance and pass the retrieved active user list to the object instance so that it will print all records present in the list.
  • System.debug: Using the debug method, we can print the details that we fetched. In the for loop, we created a user object instance and passed the user list to it. Now, using that object instance, we need to display records.
public class ActiveUserDetails {
    
    public void DisplayUserDetails(){
    
     List<User> ActiveUsers = [SELECT Id, Name, IsActive FROM User WHERE IsActive = true];
                              
     for(User userList : ActiveUsers) {
     System.debug('User ID: ' + userList.ID + ' Name: ' + userList.Name + ',  Status: ' 
                              +userList.IsActive);
      }
   }
}
SOQL for Fetch All Active User in Salesforce

In this way, we can fetch all active user details available in the Salesforce org using SOQL in Apex.

Conclusion

I hope you have an idea about the UserInfo class and its methods, which we can use to retrieve information about currently logged-in users. I then explained how to get all the details of the currently logged-in user, including the profile and license name, and how we can get it by passing a specific user ID. Then, we saw how to retrieve details of all active users from Salesforce Apex using SOQL and how to use the WHERE clause in SOQL statements.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.