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
TejTej 

User Login Info

Hi,

 

is it possible to to see whether a particular user is logged into salesforce through apex?

 

my use case is after the userId's are created , i have to check whether that the user logged into salesforceor not,

if not i have to send them a reminder email.

 

can anyine please tell me asolution to achieve this

 

Thanks much !

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

there's a lastLoginDate field on User, no need to do (a horribly expensive) query on LoginHistory.

All Answers

sandeshdsandeshd

You can try this

 

get the user id of the user by querying based on username like

user usr = [select id, username from user where username=: 'xyz@sf.com'];

 

LoginHistory[] logHist = [select logintime from LoginHistory where userid =:  usr.id];

If(logHist.size() > 0)

By checking the size of the login history you can decide 

 

 

 

 

 

SuperfellSuperfell

there's a lastLoginDate field on User, no need to do (a horribly expensive) query on LoginHistory.

This was selected as the best answer
TejTej

Thanq Simon and Sandeshd, Appriciate your help.

sandeshdsandeshd

Hi Simon,

 

I saw it in the user details page but I didnt find it in fields section under user. so I gave that query. Can you please let me know where can I find that field details

 

 

EIE50EIE50

Hi,

 

In your user list page, create a new view and add the last login date to that view. So whenever you select that view you can find last login date for all users.

 

Thanks.

sandeshdsandeshd

ok but how do we do it programatically through apex...pls suggest

SuperfellSuperfell

select id, lastLoginDate from user

TejTej

SELECT Username, LastPasswordChangeDate, LastLoginDate FROM User