Dynamics 365 Business Central: How to get your last login info (Penultimate login DateTime)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip of Business Central, how to get your last login info (Penultimate login DateTime), hope this will help.

For example, we can find the users’ last logon datetime to their mailboxes in Exchange admin center as below.

Is there a similar function in Business Central?
Yes. I believe many people have noticed that the last login time of the current user is displayed under the My Settings page.

If you look into this part of the stand source code, you can find codeunit 9026 “User Login Time Tracker” in System Application.Source.

And there is a procedure called GetPenultimateLoginDateTime() to returns the penultimate login DateTime of the current user.

Assume that the requirement is to display the last login time of the user when the user logs in to Business Central.

So refer to the standard source code, you can easily do like following.

codeunit 50100 TestCodeunit
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::LogInManagement, 'OnAfterLogInStart', '', false, false)]
    local procedure ShowLastLoginInfo()
    begin
        Message(GetLastLoginInfo());
    end;

    local procedure GetLastLoginInfo(): Text
    var
        UserLoginTimeTracker: Codeunit "User Login Time Tracker";
        LastLoginDateTime: DateTime;
    begin
        LastLoginDateTime := UserLoginTimeTracker.GetPenultimateLoginDateTime;
        if LastLoginDateTime <> 0DT then
            exit(StrSubstNo(MyLastLoginLbl, LastLoginDateTime));
        exit('');
    end;

    var
        MyLastLoginLbl: Label 'Your last sign in was on %1.', Comment = '%1 - a date time object';
}

Test video:

Next, what if you want to check the login time of other users.

Also in System Application.Source, you can find table 9008 “User Login”.

So you can force Business Central to run the “User Login” table by adding the ?table=9008 parameter to the URL, such as in the following example: 
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox02?table=9008

Then user login information will be displayed.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました