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
Arun MKArun MK 

How to identify the logged in user id, when the User has logged in as another User


I have 2 users A and B. User A has Admin profile and so has the option to login as other users.

Now lets say, User A logged into the system and logged in as User B. 
Now, when I try to get the loggen in user Id using Userinfo.getUserId() or any other apex method, I get the User B record id. 

Is there a way, I can get the actual logged user i.e., User A record Id
Hi Arun,

UserInfo.getUserId() - returns the current User Id

User Usr = new User();
Usr = [SELECT Phone, Id FROM User WHERE Id = : UserInfo.getUserId()];

the above code will return the current user's Phone and Id who execute this code.

go through the below links:

Arun MKArun MK
Hi Rockzz,

Thanks for your reply. 

If we try Userinfo.getUserId(), it just returns the User B record id. But what I want is the User A record id.

Cory CowgillCory Cowgill
I don't think you can access that information programatically via Apex.

The only location where this is available to my knowledge is in the Audit Log, which you can download as a CSV file, but can't get in Apex / Visualforce.

User-added image
umesh atryumesh atry
you can check profile by User.
Marco Sozzi 5Marco Sozzi 5
did you find a solution for this problem? I have the same problem and want to know when a sysadmin log in as an another user. 

Thank you.
Akshay DixitAkshay Dixit
Hi @Marco/ Arun,
Did you find a solution for this problem? 

Manmohan SinghManmohan Singh
Hi Arun,
you an try below option to find this manually,

There is the Setup Audit Trail which tells you who logged in as another person.
Setup > Security Controls > View Setup Audit Trail

Hi Arun,
Did you find any solution for this problem??