Finding the Email of the Founder in Salesforce When “Logging in as” a Different User | by Denis Bélanger | Jul, 2024 | Medium

PHOTO EMBED

Thu Sep 12 2024 05:44:34 GMT+0000 (Coordinated Universal Time)

Saved by @WayneChung

public with sharing class ImpersonationUtil {
    public static String getImpersonatorEmail() {
        String query = 'SELECT CreatedById FROM AuthSession WHERE UsersId = :UserInfo.getUserId() AND SessionType = \'SubstituteUser\' ORDER BY CreatedDate DESC LIMIT 1';
        AuthSession session = Database.query(query);
        if (session != null) {
            User creator = [SELECT Email FROM User WHERE Id = :session.CreatedById LIMIT 1];
            return creator.Email;
        }
        return null;
    }
}
content_copyCOPY

https://medium.com/@tempmailwithpassword/finding-the-email-of-the-founder-in-salesforce-when-logging-in-as-a-different-user-bea709883ad2