Get User Or Logged-In User Details Using SOQL

As a Salesforce developer, I was required to display the details of the currently logged-in user or other users in the Salesforce org, such as their name, email, and profile, within 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 the current user’s details.

Here, I will explain how to get user or logged-in user details using SOQL (Salesforce Object Query Language). Additionally, I will explain other user-related queries so that we can retrieve users’ details.

Get User Info Using SOQL Query

We can use SOQL to retrieve user or current user details in Apex and add conditions to retrieve the specific records we want.

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

Example: Basic SOQL to Retrieve User Details

For example, we want to retrieve all user details from the user object using an SOQL query.

SELECT Id, Name, Email, Username, IsActive FROM User

In the above SOQL query, we retrieved all user details who are present in the Salesforce org. In the SELECT clause, we need to specify the fields we want to include in the query result, and then in the FROM clause, we need to provide the object name.

Get User Or Logged-In User Details Using SOQL

Example: Retrieve User Details With WHERE Clause in SOQL

Now, we will understand when we want only those users who are active in our Salesforce org. Then, we need to add conditions to get only active users.

SELECT Id, Name, Email, Username, IsActive FROM User WHERE IsActive = 'True'

In the above SOQL query, we added the WHERE clause, which is used to add conditions to the records. Here, we only wanted active users, so using this clause, we retrieved only the active users from the result.

Retrieve User Details With WHERE Clause in SOQL

Example: Display All Users By Grouping Their Profiles Using SOQL Query

In a Salesforce org, there are multiple licenses, and when we create any profile using a license, we can add multiple users to that profile.

Now, when we want to display all users with their respective profiles, we need to group users with assigned profiles in the SOQL query.

Let’s see how to create an SOQL query to retrieve all users, grouping their respective profiles, and display the retrieved results.

SELECT COUNT(Id), Profile.Name FROM User GROUP BY Profile.Name

In the above SOQL query, we used the aggregate function to group the user records by profile name. Because SOQL does not support grouping or aggregate functions in the same way as SQL, it retrieves rows directly without applying grouping or aggregate functions.

When we want to get all User records along with their Profile.Name, we need to use the aggregate function along with the GROUP BY clause.

In the image below, you can see the number of users displayed in the ‘Count (Id)’ column and their respective profile names, which are shown in the ‘Name’ column, labeled as ‘Profile’.Name field.

Display All Users By Grouping Their Profile

Example: Display Users Created in the Last Three Months Using SOQL Query

Now we will understand how to display user details created within the last 3 months. This means that whenever we create new users and want to retrieve their details, we need to use the CreatedDate field to obtain them.

SELECT Id, Name, CreatedDate FROM User WHERE CreatedDate = LAST_QUARTER

In the above SOQL query, we added the CreatedDate field, and we want users’ details for those created in the last 3 months. For that, we added a condition using the WHERE clause, and then, using the LAST_QUARTER date literals, we provided a time period.

Display Users Created in Last Three Months

Example: Display Users With Last Login Date Using SOQL Query

When we want to retrieve users’ records to check their last login date and display documents in descending order in the query results, we need to use the ORDER BY clause to sort the results in descending order.

SELECT Id, Name, LastLoginDate FROM User WHERE LastLoginDate != NULL ORDER BY LastLoginDate DESC

In the user object, we have a ‘last login date’ field, which displays the most recent login date for that particular user. In the above SOQL query, we utilized the LastLoginDate field to display users’ recent login dates.

Then, we also added a condition using the WHERE clause not to display null LastLoginDate and to display the query result in descending order; we applied the ORDER BY clause in SOQL.

Display Users With Last Login Date Using SOQL Query

Example: Display Users With Roles and Profile Using SOQL Query

Using SOQL query, we cannot directly display roles and profiles because they are the lookup with the user object. When we want to display values from parent or child objects, we can use Salesforce Relationship Queries in SOQL to retrieve records from child to parent or parent to child.

When we want to display users with roles and profiles, we need to retrieve records from child to parent, so the SOQL query would be as follows.

SELECT Id, Name, UserRole.Name, Profile.Name FROM User

In the above SOQL query, we added fields from the user object, and then for the role and profile name, we used dot (.) notation to retrieve a value from the parent object to the child object.

Display Users With Roles and Profile Using SOQL Query

Current or Logged-In User in Salesforce

In Salesforce, the current user is actively logged in, interacts with the Salesforce UI, and performs operations such as creating records, managing data, and automating processes.

When creating a new user in Salesforce, we must fill out several mandatory fields, including name, alias, email, username, profile, time zone, and other relevant details.

Example: Get Details of Current Logged-In User Using SOQL Query

We cannot retrieve the user details for the currently logged-in user directly from the SOQL editor. Instead, we can use the SOQL query in the Apex class, and after executing that Apex class, we can retrieve the currently logged-in user details.

The reason behind this is that to get the currently logged-in user ID, we need to use the UserInfo class in the Apex 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 );           
    }
}

In the above Apex class, we created a SOQL query to retrieve the profile name and license name from the currently logged-in user and stored it in a variable.

Get Current User Profile Name and License in Salesforce Apex

In this way, we can get the current user details using SOQL and Apex classes.

Conclusion

I hope you have got an idea about how to get user or logged-in user details Using SOQL (Salesforce Object Query Language). Additionally, I have explained other user-related queries to retrieve the user’s details. We have seen how to use different clauses to filter the user record results that we get from the SOQL query.

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.