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
Tad Aalgaard 3Tad Aalgaard 3 

After using "log in to community as user" how can I use Apex to get the original Salesforce user?

I have a need to get the User object for the user who logged into SFDC so that I can use it in my community. After using "log in to community as user" how can I use Apex to get the original Salesforce user?
Best Answer chosen by Tad Aalgaard 3
Mikola SenykMikola Senyk
It is impossible. There is no relation between salesforce user and community user.

However you can use some trick. Idea is the following:
We login in as community user from Contact page. At this page we have to save somewhere salesforce user ID and community user ID. It could be custom settings, web storage or something similar. In landing community page we can read this data and to detect user ID.
Of course tricky solution has a lot of limitations. For example you have to separate real community user and "fake" ones (who have logged from Contact page).

All Answers

Mikola SenykMikola Senyk
It is impossible. There is no relation between salesforce user and community user.

However you can use some trick. Idea is the following:
We login in as community user from Contact page. At this page we have to save somewhere salesforce user ID and community user ID. It could be custom settings, web storage or something similar. In landing community page we can read this data and to detect user ID.
Of course tricky solution has a lot of limitations. For example you have to separate real community user and "fake" ones (who have logged from Contact page).
This was selected as the best answer
Piotr GajekPiotr Gajek

Check it: 

https://salesforceprofs.com/how-to-detect-log-in-to-community-as-user-in-apex/