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
Eric Kendi ShiraishiEric Kendi Shiraishi 

In Apex, how do I get the current user login information?

Hi,

My case is that I need to know which application (field on login history) the current user is using inside my trigger.
I checked the System.UserInfo methods, but it does not have the right information, is there any other class/method I can use to get the full login information on the current user?

Thanks.
Best Answer chosen by Eric Kendi Shiraishi
Eric Kendi ShiraishiEric Kendi Shiraishi
I managed to get the correct info that I wanted.
If you use the Auth.SessionManagement.getCurrentSession(), you can get the map with the right login history id and after that, you can use the SOQL to get only the login history you need.

All Answers

Jim JamJim Jam
maybe try ...

LoginHistory lh = [select Application from LoginHistory where Userid = :system.userInfo.getUserId() order by LoginTime desc limit 1];
system.debug(lh.Application);
Eric Kendi ShiraishiEric Kendi Shiraishi
Thanks for the answer, but I would like to have another option without SOQL on LoginHistory table, as it is an Integration User and can be accessed by multiple applications.
Eric Kendi ShiraishiEric Kendi Shiraishi
I managed to get the correct info that I wanted.
If you use the Auth.SessionManagement.getCurrentSession(), you can get the map with the right login history id and after that, you can use the SOQL to get only the login history you need.
This was selected as the best answer