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
srikanth123srikanth123 

Update custom field when user logs in and logs out

I have a custom field on the user object (status__c). Whenever a user logs into salesforce I should update the status as 'Available' and whenever the user logs out I should update the status as 'Unavailable'. Any ideas on how to achieve this?

NBlasgenNBlasgen

Best I might be able to suggest is something that tells you the last time the person was available rather than Available and Not Available.

 

The left hand side navigation (Recent Items, etc) can have custom code placed on it and for the most part, that pannel is always rendered (even if it's hidden I believe).  So how about an AJAX request sent every 10 seconds (or so) from that pannel to a VF page that marks a custom field as being last updated NOW().

 

To find the people who are currently online, select from your custom table WHERE last_update is less than 20 seconds (10 seconds plus a little buffer).  To find those people not around, select where last_update is greater than 20 seconds.  Also, you might be able to have a formula field that sets Available and Not Available based on that last_update.  IF (last_update - NOW() > 20) 'Not Available' else 'Available' or whatever the proper use of a formula field would be.

srikanth123srikanth123

For implementing your solution i foresee the following hurdles.

1) Users can choose to display the custom home page components on all tabs or only on the home tab. So if the user chooses to show the home page components only on the home tab then the solution will not work.

2) What if the user is on setup page for long time then even though the user is logged in the solution will show him as unavailable.

 

So I am searching for solution to overcome these problems also.

NBlasgenNBlasgen

#1 I don't feel is that big of a problem

 

#2 is an issue and I don't have any suggestions for that.  You could instead move to ClientSide javascript but then we're just going into the non-realistic part of the world.

 

So what you really need is a longer timeout based on my original suggestion but also the ability to time it out sooner if the person logs out manually.  So a custom logout screen would fix that but I'm not aware of how to create a custom logout screen, especially if this screen is also supposed to be available fromt he Setup screen.

 

I wish you luck.