Dynamics 365 Business Central: Check whether a user has logged in to the current environment (to any of the companies)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to check whether a user has logged in to the current environment (to any of the companies).

As you might know, after user accounts are created in the Microsoft 365 admin center, there are two ways to import them to Business Central:

1. A user account is imported automatically when the user signs in to Business Central the first time.

2. The administrator can import users by choosing the Update Users from Microsoft 365 action on the Users page. More details: To add users or update user information and license assignments in Business Central

There is a big difference here, in the first method, the user has logged in to the Business Central, but in the second method, the user has not logged in. So after the users are synchronized, is there any way to check which users have logged in to the system and which have not? If some users have not logged in for a long time, consider removing the user’s license.

In table 2000000120 User, there is no field to determine whether the user has logged in.

Actually, we can use User Login History functions, such as Register Time and Sign-in logs, we can filter out users who have not logged in. More details: User Login History (Register Time and Sign-in logs). However, the Register Time function must be enabled first, and the Sign-in logs function is outside the system.

So in this post, I would like to share another simple way for developers.

We can just use procedure UserLoggedInEnvironment(UserSecurityID: Guid) in codeunit 9026 “User Login Time Tracker”.

Checks whether a user has logged in to the current environment (to any of the companies) in the past.

    /// <summary>
    /// Checks whether a user has logged in to the current environment (to any of the companies) in the past.
    /// </summary>
    /// <param name="UserSecurityID">The User Security ID.</param>
    /// <returns>True if the user has logged in to any of the companies; otherwise - false.</returns>
    procedure UserLoggedInEnvironment(UserSecurityID: Guid): Boolean
    var
        UserLoginTimeTrackerImpl: Codeunit "User Login Time Tracker Impl.";
    begin
        exit(UserLoginTimeTrackerImpl.UserLoggedInEnvironment(UserSecurityID));
    end;

Let’s see a simple example. On the Users page, add a new action to check whether the currently selected user has logged into the system.

Source Code:

pageextension 50112 ZYUsersExt extends Users
{
    actions
    {
        addafter("Update users from Office")
        {
            action(CheckUserLoggedInEnvironment)
            {
                ApplicationArea = All;
                Caption = 'Check User Logged In Environment';
                Image = Check;
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    UserLoginTimeTracker: Codeunit "User Login Time Tracker";
                begin
                    if UserLoginTimeTracker.UserLoggedInEnvironment(Rec."User Security ID") then
                        Message('The selected user has logged in to the current environment')
                    else
                        Message('The selected user has not logged in to the current environment');
                end;
            }
        }
    }
}

The above is the way to check it in code, if you want to display it in a list you can do that as well.

Source Code:

pageextension 50112 ZYUsersExt extends Users
{
    layout
    {
        addafter("User Name")
        {
            field(UserLoggedIn; UserLoggedIn)
            {
                Caption = 'User Logged In';
                ApplicationArea = All;
            }
        }
    }

    var
        UserLoggedIn: Boolean;

    trigger OnAfterGetRecord()
    var
        UserLoginTimeTracker: Codeunit "User Login Time Tracker";
    begin
        UserLoggedIn := UserLoginTimeTracker.UserLoggedInEnvironment(Rec."User Security ID");
    end;
}

Very simple, it is recommended to clean up unlogged users regularly. Give it a try!!!😁

PS:
1. This standard method uses the following table, but because the Access property is set to Internal, we can’t access it directly. More details: Can we access the standard internal table/field (Access Property = Internal) via AL???
table 9011 “User Environment Login”:

2. Check whether a user has a specific permission set assigned

3. How to check if a user is Delegated Admin via AL

4. How to quickly check if the user has the SUPER permissions set via AL (Best practice)

5. User Security Id (SID) and User Id (Name)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL