Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please note that I don't want to know the interval between logins - I want to know how many minutes (or hours) elapsed between a login and the subsequent logout. Is it possible to calculate this in SFDC, using any combination of technologies?

share|improve this question
lastlogintime - timenow will be the formula right? by querying the loginhistory, I found the latestlogin and subtracted it from the current time and got the number of hours the user is logged in ? won't that help? – Rao Dec 20 '12 at 19:09
@rao that wouldn't work because you need the logout time too, right? – Ryan Guest Dec 20 '12 at 19:57
@RyanGuest ah i thought the question was to see how long the user was logged in :/ – Rao Dec 20 '12 at 21:28
up vote 2 down vote accepted

The logout times are not recorded so unless you wrote a browser extension or tied a JavaScript event to the click of the logout button , im afraid it isn't possible to retrospectively calculate the logged in duration of the user.

You could have a sort of 'heartbeat' embedded in the sidebar running as a JavaScript polled job I suppose which kept writing / updating a user specific record in a custom object ever so often while a user was logged in. Every time a user logged out there would be a significant pause in the writing of these 'active' records .

share|improve this answer
That's very interesting. Could you point me to an example? I'm having a bit of trouble visualizing a "heartbeat". – John Westenhaver Dec 24 '12 at 20:00

Open your debug log and paste this :

You can create a page use this as a controller and display the difference to the user(or) use it apex code as a method:

List<Loginhistory> login = [SELECT UserId, LoginTime from LoginHistory where userid=: userinfo.getuserid() and logintime = today order by logintime asc];
    decimal  numberofhours;
    decimal numberofmiuntes;
    integer size = login.size();
    numberofhours = ( - login[size-1].LoginTime.hour()  );
    numberofmiuntes = ( - login[size-1].LoginTime.gettime())/60000;  
    system.debug('Hours: '+numberofhours + ' Minutes:'+ numberofmiuntes);
share|improve this answer
This is the number of hours and minutes the current user has been logged in, while this user is still logged in. After this user logs out, I'd like to know how long the user WAS logged in. The heartbeat idea above is very interesting but I'm not sure how to build it in a way that doesn't eat up too much bandwidth ... – John Westenhaver Dec 24 '12 at 20:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.