+ Start a Discussion
Raj R.Raj R. 

How would i determine if user has logged in within last 24hours?

Hi,

We have a need to schedule a batch that will perform some action if the user has not logged in in the last 24hours. On the User object, there is a LastLoginDate field which is a DateTime field. 

I want to write a SOQL query that will filter out (not select) users who have logged in with last 24hours. I am having difficulty understanding the correct syntex and need help determining what would be the best approach. What would be the recommended way to do this?
 
/*
 *i tried this but it was giving very weird response.
  *if today = 3/30/2017, when i do the following below i see that in the debug statements that it pulling users with a lastlogindate of 2017-03-30T07:XX:XX values
 * if i print DateTime.Now() i am getting a result of 2017-03-31T01:05:53 (if i were to run it on 3/30/2017 6:05pm PT
  */
List<User> usrs = [Select Id, LastLoginDate, IsActive, Name 
                                  From User
                                  Where LastLoginDate = TODAY];

The goal is to get the set of records where all user.lastlogindate >= 3/29/2017T18:05:53 and user.LastLoginDate <=3/30/2017T18:05:53 (basically within the last 24hours when the logic is ran). Any thoughts?
Best Answer chosen by Raj R.
LBKLBK
Hi rr,

This query will help you fetching the users who logged in the last 24 hours (until the current minute you are running this query).
 
List<User> usrs = [Select Id, LastLoginDate, IsActive, Name  
                                  From User
                                  Where LastLoginDate >= :System.Now().addDays(-1)];
Let me know if this helps.
 

All Answers

brahmaji tammanabrahmaji tammana
Hi,

Can you try this way ?
 
select id,lastlogindate from user where lastlogindate >= 2017-03-29T18:05:53Z
and lastlogindate <=2017-03-30T18:05:53Z

Thanks
Brahma

 
LBKLBK
Hi rr,

This query will help you fetching the users who logged in the last 24 hours (until the current minute you are running this query).
 
List<User> usrs = [Select Id, LastLoginDate, IsActive, Name  
                                  From User
                                  Where LastLoginDate >= :System.Now().addDays(-1)];
Let me know if this helps.
 
This was selected as the best answer
@GM@GM
Hi rr,

If the above solution doesn't satisfyies your requirement then you can use loginHistory or AuthSession syatem object to get the same info.
 
System.debug('Session created in last 24hrs(one day) '+[SELECT CreatedDate,Id,LastModifiedDate,LoginGeoId,LoginHistoryId,LoginType,LogoutUrl,NumSecondsValid,ParentId,SessionSecurityLevel,SessionType,SourceIp,UsersId,UserType FROM AuthSession where CreatedDate >= :System.Now().addDays(-1)]);

Regards,
GM
Sumit Sharma 186Sumit Sharma 186
Hi

You can try this

datetime lastdt = System.now()-1;

List<User> t=[Select Id, LastLoginDate, IsActive, Name From User
                               Where LastLoginDate < :lastdt ];


Sumit Sharma
Mirketa Inc (http://www.mirketa.com/)