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
ShekharGawaliShekharGawali 

Last Login Date for current logged in User

Hi,

My requirement is to show logged in users all the articles published after their respective last login date.
Since, the last login date is updated as soon as user logs in, I'm unable to get the date prior to that.
I tried using LoginHistory object, but for running soql on LoginHistory object, "Manage Users" permission is required. Since this permission can not be provided to all the users, this can not be considered as option.
Please find the code below:
//CurrentLoginTime as user is already logged in
DateTime currentLogin = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()].LastLoginDate ; 
//Any prior login time before the current login time
DateTime lastLogin = [select loginTime from LoginHistory where userId = :UserInfo.getUserId() and loginTime != :currentLogin order by LoginTime Desc limit 1].loginTime;
I have also tried to make the class "without sharing", it throws an exception.
Many thanks in advance for the help.

Regards,
Shekhar
 
Best Answer chosen by ShekharGawali
ShekharGawaliShekharGawali

Thanks Eswar for the quick response.

Mostly I prefer trigger as the last option, so I was looking workarounds.
And I found answer to my own question as:
1) Create two custom date fields on user object, prefrebly create a new object with user lookup if you want to use this for multiple pages.
Date 1 : To store previous login date
Date 2 : Current Login date
2) As soon as the page loads, check if Current login date (Date 2 field) is not today update the fields as
Date1 : Current Login Date (Date 2 Field)
Date2: Today
Note: You might want to use action attribute of page instead of constructor to avoid "dml currently not allowed" error
3) Fetch the articles based on Date1 field

All Answers

eswar prasadeswar prasad
HI Shekhar,
  Pls try below code:

trigger lastloginDate on Mason_Object__c(before update)
{
User u = [SELECT LastLoginDate FROM User WHERE Id =:UserInfo.getUserId()];
for(Mason_Object__c c : Trigger.new){
//May want to put some condition checks
c.LastLogin__c = u.LastLoginDate;
}
}

Note: If this helps you, please mark the question as answered. Thus helping others too.
Regards, 
Eswar Prasad.

 
ShekharGawaliShekharGawali

Thanks Eswar for the quick response.

Mostly I prefer trigger as the last option, so I was looking workarounds.
And I found answer to my own question as:
1) Create two custom date fields on user object, prefrebly create a new object with user lookup if you want to use this for multiple pages.
Date 1 : To store previous login date
Date 2 : Current Login date
2) As soon as the page loads, check if Current login date (Date 2 field) is not today update the fields as
Date1 : Current Login Date (Date 2 Field)
Date2: Today
Note: You might want to use action attribute of page instead of constructor to avoid "dml currently not allowed" error
3) Fetch the articles based on Date1 field

This was selected as the best answer