+ Start a Discussion
dcarmen66dcarmen66 

In Apex, is it possible to determine if a User is logged in through the API?

I'm exploring some situations where we might want different actions depending on if a user is logged in through the UI versus connecting via the API. Is there any way in Apex to determine this?

Satish_SFDCSatish_SFDC
I dont think there is a direct method.
One alternative would be to query the LoginHistory object of the Salesforce filtering by UserID and ordering by the Login Time in Descending order.
This way you can then take the first record as this would be the last login of the user.
Once you get the record you can get the information through various fields - APIType, Browser, Application etc.

More info:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_loginhistory.htm

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
GunishGunish

Hello, 

 

Currently It is not possible to check the user's status weather he is logged in or not. 

You can use the following calls to get other information about the user. 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_UserInfo_static_methods.htm

 

 

There is a workaround that we have used in the past, but that was for a very specific requirement, 

We created a landing page for the user, which was a visualforce page, and onload of the visualforce page, we made an entry into a custom object to store the last login time : 

Similar to this post here  :

http://salesforce.stackexchange.com/questions/10995/is-a-user-logged-in

 

 

But ultimately you are looking for this object to check when the user has loggedin, (No Logout time available though)

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_loginhistory.htm

 

Sorry, could not be of more help!

If you do find a way, do share it with us. 

 

Cheers!

Gunish Rai Chawla

 

_____

If this post was helpfull, do remember to give me Kudos!

 

 

 

 

Anmol Bali 2Anmol Bali 2
HI Guys,

I have found a way to detect impersonated user, please visit the below blog
https://anmolbali.wixsite.com/detectimpersonation
Please mark the answer if you fill its right.