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
Ajith Anand2Ajith Anand2 

SOQL Query for querying Loginhistory

Hi,

I have a LIST of userIds from the User object. In the Loginhistory object, userId is the reference field.
The LoginHistory maintains the login activities for all the users. Hence there are multiple record entries for each user.

How can I query and build a new LIST of the most recent LoginTime (one record per UserId) for each UserId, ensuring that the salesforce best practices are respected?
Any help would be greately appreciated.

Thanks,

Ajith
Best Answer chosen by Ajith Anand2
surasura
just query the user object it has a field  lastloginDate  which shows  date and time when the user last successfully logged in. This value is updated if 60 seconds have elapsed since the user’s last login. 
SELECT id,Name, LastLogindate FROM User

 

All Answers

surasura
just query the user object it has a field  lastloginDate  which shows  date and time when the user last successfully logged in. This value is updated if 60 seconds have elapsed since the user’s last login. 
SELECT id,Name, LastLogindate FROM User

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
For login History you can try below link:-

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_loginhistory.htm
 
SELECT ApiType,ApiVersion,Application,Browser,ClientVersion,Id,LoginTime,LoginType,LoginUrl,NetworkId,Platform,SourceIp,Status,UserId FROM LoginHistory

Simple query showing UserId & LoginTime for each user
SELECT UserId, LoginTime from LoginHistory;

Query showing logins only after a specified date and time
 
SELECT UserId, LoginTime from LoginHistory WHERE LoginTime > 2010-09-20T22:16:30.000Z;



Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help