+ Start a Discussion
MJ Kahn / OpFocusMJ Kahn / OpFocus 

Get a list of users currently logged into my org

I need to get a list of all users who are currently logged into my org.

This idea (https://success.salesforce.com/ideaView?id=08730000000BpPOAA0) explains that, as of a few months ago, this is possible. It explains where to find this information via the browser (Setup | Security Controls | Session Management), and also says that it's available through the API. Can anyone point me to documentation that explains how to get it via the API?

Thanks!
Best Answer chosen by MJ Kahn / OpFocus
BalajiRanganathanBalajiRanganathan
You can use AuthSession Object to query using SOQL or SOAP or REST API

Select a.UsersId, a.UserType, a.SourceIp, a.SessionType, a.SessionSecurityLevel, a.ParentId, a.NumSecondsValid, a.LoginType, a.LastModifiedDate, a.Id, a.CreatedDate From AuthSession a

All Answers

BalajiRanganathanBalajiRanganathan
You can use AuthSession Object to query using SOQL or SOAP or REST API

Select a.UsersId, a.UserType, a.SourceIp, a.SessionType, a.SessionSecurityLevel, a.ParentId, a.NumSecondsValid, a.LoginType, a.LastModifiedDate, a.Id, a.CreatedDate From AuthSession a
This was selected as the best answer
Lee ElkinsLee Elkins
I do see the data I want, but there is a field called NumSecondsValid that is equal to 14400 (4 hours).  That means I am getting values returned for many hours after a user has acutally logged off.  Is there another way around this, or way to reduce the amount of time this authorization is for?
MJ Kahn / OpFocusMJ Kahn / OpFocus
Per the documentation for NumSecondsValid, it's "The number of seconds before the session expires, starting from the last update time." LastModifiedDate gives the date/time the session was last active. Add NumSecondsValid to that, and it will tell you when the session will time out, assuming there's no further activity. The object doesn't have records for sessions that have expired, and the object isn't writable - you can't edit NumSecondsValid or anything else. 
AK2017AK2017
when i used this query, Eclipse stopped me saying AuthSession is not supported type. Can you please help me. i am trying to use this in Apex class.
MJ Kahn / OpFocusMJ Kahn / OpFocus
Aariff, what version of the Force.com plug-in for Eclipse are you using? I'm using a version from Spring 15 (33.0), and I can get to AuthSession just fine.
AK2017AK2017
Even i tried pating this in Apex class, it is throwing me same error. Version Spring 16.
 
List<ID> lstids = new List<ID>();
        for(Agent_Assignment__c AA:lstInquiry){
        	lstids.add(AA.Agent__c);
        }
        
        List <AuthSession> lstAuth = new List<AuthSession>();
        
        lstAuth =[Select UsersId, UserType, SourceIp, SessionType, SessionSecurityLevel, ParentId, NumSecondsValid, LogoutUrl, LoginType, LoginHistoryId, LastModifiedDate, Id, CreatedDate From AuthSession where LoginType = 'Application' and UsersId IN: lstids and SessionType = 'UI' order by LastModifiedDate DESC];
        			
         for(Agent_Assignment__c AA:lstInquiry){
         	for(AuthSession AuS:lstAuth){
         	if(AA.Agent__c == Aus.UsersId){
         		AA.Temp_login_updated_date__c = Aus.LastModifiedDate;
         		system.debug('user id'+AA.Agent__c);
         		system.debug('user Name'+AA.Agent__r.Name);
         		system.debug('Last modified user date'+AA.Temp_login_updated_date__c);
         		system.debug('Last modified user date on Auth'+Aus.LastModifiedDate);
         		break;
         	}         	
         	}
         	
         }

 
MJ Kahn / OpFocusMJ Kahn / OpFocus
I don't have your Agent_Assignment__c object, of course, but when I run the following,it runs just fine:
 
List <AuthSession> lstAuth = new List<AuthSession>();
        
lstAuth =[Select UsersId, UserType, SourceIp, SessionType, SessionSecurityLevel, ParentId, NumSecondsValid, LogoutUrl, LoginType, LoginHistoryId, 
          LastModifiedDate, Id, CreatedDate 
          From AuthSession 
          Where LoginType = 'Application' and SessionType = 'UI' order by LastModifiedDate DESC];
        			
for(AuthSession AuS:lstAuth){
    System.debug(JSON.serializePretty(AuS));
}
Are you getting your error at compile-time or at run-time? If at compile-time, what's the API version of your class/trigger? AuthSession is supported starting in version 29.0.
 
AK2017AK2017
Can you please post your code for me to see on how you have used it?  
MJ Kahn / OpFocusMJ Kahn / OpFocus
I ran the above in an Anonymous block. You could wrap it in a class to run it. Just make sure that the API version of your class is >= 29.0.
AK2017AK2017
Thanks MJ. Though my environment was Spring 16, the apoex class was version 22 and it did not let me save. I changed the version and i was able to save it.

Thanks again. 
Jing HeJing He
This AuthSession object is not available for report type.  How can I create a report on it?  A few of possible options will be
1) Create a VF page to query AuthSession
2) Create a custom object to copy data from AuthSession. Then create report type on that custom object
Any other suggestion?