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

Can Apex code tell if an administrator logged in as another user?

Does anyone know a way for my Apex code to determine the difference in being logged in as a standard Salesforce user, versus an Admin user that logged in and in their admin session logged in as that user?



David Wright, Gerson Lehrman Group


Satyendra RawatSatyendra Rawat


1. Get logedIn UserId help of Id userId = UserInfo.getUserid();  

2. get the User record with help of user id 

User logedInUser = [Select u.Profile.Name, u.Profile.Id, u.ProfileId From User u where id=:userId]; 

3. check the profile name help of 

if(logedInUser.Profile.Name=='standard Salesforce')


// write code


else if(logedInUser.Profile.Name=='System Administrator')


//write code here


Chris HeathChris Heath
David, did the above code seem to do what you were looking for? To me it looks like it would just give you the Profile Name of the "Logged in as" user account.

If not, did you figure out a way to do what you were looking for?
Chris, you are correct -  Satyendra's response did not allow me to tell the difference between being logged in as that person or being an administrator impersonating that person.  I haven't found a way for my code to tell the difference, yet.
Chris HeathChris Heath
David, thank you very much for your quick reply on such an old question. I appreciate it. If I do manage to find a way, which is not looking hopeful, I'll be sure to let you know.