Want to retrieve useful details about the currently logged-in Salesforce user using Apex? Whether you’re building a user dashboard or adding logging functionality, knowing how to fetch user locale, time zone, and last login date can really come in handy.
In this blog post, we’ll walk through a clean and simple Apex method that pulls all three packaged neatly in a custom wrapper class.
If you’re just starting out in Apex, this is a perfect example to help you understand:
How to use the
UserInfoclassHow to query user data from the
UserobjectHow to return multiple values using a wrapper
Let’s get started!
🧠 What Are We Trying to Do?
We want to build a method that:
Uses built-in Salesforce functions to get locale and time zone
Uses a SOQL query to get the user’s last login date
Returns all this info together in a single custom class (wrapper)
This way, we can reuse this logic anywhere maybe for analytics, a login audit feature, or even just to display it in a custom UI component.
🔍 Code Explanation
We start with a class named ApexUseCaseTwenty, and create a static method getLoggedInUserInfo which will return all the user details wrapped inside a custom object.
We create an instance of our custom UserInfoWrapper class to hold all the data.
The UserInfo class provides built-in methods like:
getLocale()– returns the user’s locale setting (likeen_US)getTimeZone()– returns the user’s time zone (as aTimeZoneobject, which we convert to string)
We use getUserId() to fetch the ID of the current user, and then run a simple SOQL query to get their LastLoginDate.
We safely check if the record was found and if LastLoginDate is not null, and then store it in the wrapper.
Finally, we return the populated wrapper.
This nested class UserInfoWrapper holds all three values we need to return.
✅ Why This Is Useful
You can use this pattern in a variety of real-world scenarios:
Show last login and location settings in user profiles
Log user info during transactions or support requests
Dynamically customize content based on user’s time zone or locale
Plus, this approach is clean, reusable, and easy to expand — just add more fields to the wrapper if needed!
🧾 Final Code Snippet
🎥 Watch It in Action!
Want to see this in a live demo? I’ve made a short video where I walk through this method, run it in the Developer Console, and explain how each piece works.
Want to go further with this?
Learn it step by step in a course, or talk it through on a 1:1 call.
