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
UserInfo
classHow to query user data from the
User
objectHow 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
public class ApexUseCaseTwenty {
public static UserInfoWrapper getLoggedInUserInfo(){
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.
UserInfoWrapper wrapper = new UserInfoWrapper();
We create an instance of our custom UserInfoWrapper
class to hold all the data.
wrapper.localeInfo = UserInfo.getLocale();
wrapper.timezoneInfo = String.valueOf(UserInfo.getTimeZone());
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 aTimeZone
object, which we convert to string)
String userId = UserInfo.getUserId();
List<User> userRecord = [SELECT Id, LastLoginDate FROM User WHERE Id = :userId LIMIT 1];
We use getUserId()
to fetch the ID of the current user, and then run a simple SOQL query to get their LastLoginDate
.
if(userRecord.size() > 0){
if(userRecord[0].LastLoginDate != null){
wrapper.lastLoginInfo = userRecord[0].LastLoginDate;
}
}
We safely check if the record was found and if LastLoginDate
is not null, and then store it in the wrapper.
return wrapper;
}
Finally, we return the populated wrapper.
public class UserInfoWrapper{
public DateTime lastLoginInfo;
public String localeInfo;
public String timezoneInfo;
}
}
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
public class ApexUseCaseTwenty {
public static UserInfoWrapper getLoggedInUserInfo(){
UserInfoWrapper wrapper = new UserInfoWrapper();
wrapper.localeInfo = UserInfo.getLocale();
wrapper.timezoneInfo = String.valueOf(UserInfo.getTimeZone());
String userId = UserInfo.getUserId();
List<User> userRecord = [SELECT Id, LastLoginDate FROM User WHERE Id = :userId LIMIT 1];
if(userRecord.size() > 0){
if(userRecord[0].LastLoginDate != null){
wrapper.lastLoginInfo = userRecord[0].LastLoginDate;
}
}
return wrapper;
}
public class UserInfoWrapper {
public DateTime lastLoginInfo;
public String localeInfo;
public String timezoneInfo;
}
}
🎥 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.