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
harsha B R 9harsha B R 9 

How to find the currently logged in users in the organisation.

Hello Everyone,

Is there any way to find out the currently logged in users of the org.

Requirement :- When a lead is created,I need to assign the lead dynamically to only to the logged in users(Need to fetch all the logged in users of the org programatically).

Salesforce has mentioned in one of the ideas that they have implemented it - https://success.salesforce.com/ideaView?id=08730000000BpPOAA0

Executed the SOQL query - Select a.UsersId, a.IsCurrent, a.UserType, a.SourceIp, a.SessionType, a.SessionSecurityLevel, a.ParentId, a.NumSecondsValid, a.LoginType, a.LastModifiedDate, a.Id, a.CreatedDate From AuthSession a

But,it returned only my user record.It didn't not returned the other users who have logged in the orge.

All the logged in users can be accesed at the UI level --> security controls --> session Management.

Any idea/help is appreicated.

Thanks & Regards,
Harsha BR 
YogeshMoreYogeshMore
Hi Harsha,

You can try following query. test it on developer console first.
 
SELECT  Id, createddate, IsCurrent, UsersId, LogoutUrl, SourceIp FROM AuthSession WHERE IsCurrent = true

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44
GauravGargGauravGarg
Hi Harsha,

Salesforce do not ask to Query currently Logged in user, please try with this block of code:
 
id loggedInUser = Userinfo.getUserId();

It will provide you the Id of currently logged in user. If you need more fields please visit this link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm)

Thanks,
Gaurav
Skype: gaurav62990