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
Jason Smith 40Jason Smith 40 

Finding information about an impersonated user in apex

Hi all,

We are developing a canvas app in which a secure json payload is sent with various bits of information about the user accessing the app and the ID for the record that the user is viewing. Salesforce administrators can 'impersonate' other users by logging in and the information passed to our canvas app is that of the impoersonated user and not the admin doing the impersonation.

My question: Is it possible through apex to either find out that:
1) The user ID of the 'actual' user who is logged in (ie. the admin and not the impersonated user), OR
2) whether impersonation is occurring (ie. just a true/false result that says whether impersonation is occurring).

We need to incorporate special behavior for admins in our app when impersonation is occurring.

Thanks.

- Kash
Terence_ChiuTerence_Chiu
Jason,

The only method I can think of is via the Setup Audit Trail (https://help.salesforce.com/HTViewHelpDoc?id=admin_monitorsetup.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=admin_monitorsetup.htm&language=en_US)) which shows if admins are logging in as other users. Unfortunately I do not believe you can access the audit trail via Apex natively. There is an idea on adding this functionality (https://success.salesforce.com/ideaView?id=08730000000glm9AAA).

There is a way to scrap the data from the audit file which can be found on the below link.

https://www.safaribooksonline.com/blog/2013/11/04/auditforce-native-surfacing-of-the-salesforce-setup-audit-trail/
Anmol Bali 2Anmol Bali 2
Hi Guys, 

Through Audit trail you wont able to access login hostory of impersonated user, whereas you can view records in audit trail
Please visit my blog, its a 100% tested solution.
https://anmolbali.wixsite.com/detectimpersonation 
Please mark my answer, if you feel its the right one.