Dynamics 365 Business Central: How to get a user’s license type from AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to get a user’s license type from AL, such as Dynamics 365 Business Central Premium, Dynamics 365 Business Central Essential, Internal Administrator, Dynamics 365 Business Central Team Member, and so on.

First, let’s take a look at where these license types are stored.
On the Users page and User Card page, you can find the license types displayed in User Plans FactBox (9826, ListPart), and the source table is User Plan (9005).

But this table cannot be opened directly.

You do not have the following permissions on TableData User Plan: Read.

If you see the source code, you can find the Access property of the table is Internal.
Access = Internal;

ValueDescription
PublicThe object can be accessed by any other code in the same module and in other modules that references it.
InternalThe object can be accessed only by code in the same module, but not from another module.

So you can’t use this table directly in your extension either.

‘User Plan’ is inaccessible due to its protection level

Microsoft does not allow users to access the User Plan table, perhaps for some security reasons, but has prepared another way to access the data in the User Plan table.

query 774 “Users in Plans”:

You can force Business Central to run the query by adding the ?query=774 parameter to the URL, such as in the following example: 
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox?query=774

Let’s do a simple test.

Source Code:

pageextension 50123 MyExtension extends "Customer List"
{
    trigger OnOpenPage()
    var
        UsersInPlans: Query "Users in Plans";
    begin
        UsersInPlans.SetFilter(UsersInPlans.User_Name, UserId);
        UsersInPlans.Open();
        while UsersInPlans.Read() do begin
            Message(UsersInPlans.Plan_Name);
        end;
    end;
}

Test video: You can get the current user’s license types from AL.

Next, let’s try to display the user plan list on a new page.

Test video:

Source Code:

table 50123 UserPlan
{
    TableType = Temporary;
    DataClassification = CustomerContent;

    fields
    {
        field(1; "User Security ID"; Guid)
        {
            Caption = 'User Security ID';
            DataClassification = CustomerContent;
        }
        field(2; "Plan ID"; Guid)
        {
            Caption = 'Plan ID';
            DataClassification = CustomerContent;
        }
        field(10; "User Name"; Code[50])
        {
            Caption = 'User Name';
            DataClassification = CustomerContent;
        }
        field(11; "Plan Name"; Text[50])
        {
            Caption = 'Plan Name';
            DataClassification = CustomerContent;
        }
        field(12; State; Option)
        {
            Caption = 'User State';
            OptionCaption = 'Enabled,Disabled';
            OptionMembers = Enabled,Disabled;
            DataClassification = CustomerContent;
        }
    }

    keys
    {
        key(Key1; "Plan ID", "User Security ID")
        {
            Clustered = true;
        }
    }
}

page 50123 UserPlans
{
    Caption = 'User Plans';
    PageType = List;
    UsageCategory = Lists;
    ApplicationArea = All;
    SourceTable = UserPlan;

    layout
    {
        area(Content)
        {
            repeater(Group)
            {
                field("User Security ID"; Rec."User Security ID")
                {
                    ToolTip = 'Specifies the value of the User Security ID field';
                    ApplicationArea = All;
                }
                field("User Name"; Rec."User Name")
                {
                    ToolTip = 'Specifies the value of the User Name field';
                    ApplicationArea = All;
                }
                field("Plan ID"; Rec."Plan ID")
                {
                    ToolTip = 'Specifies the value of the Plan ID field';
                    ApplicationArea = All;
                }
                field("Plan Name"; Rec."Plan Name")
                {
                    ToolTip = 'Specifies the value of the Plan Name field';
                    ApplicationArea = All;
                }
                field(State; Rec.State)
                {
                    ToolTip = 'Specifies the value of the User State field';
                    ApplicationArea = All;
                }

            }
        }
    }
    trigger OnOpenPage()
    var
        UsersInPlans: Query "Users in Plans";
    begin
        if UsersInPlans.Open() then begin
            while UsersInPlans.Read() do begin
                Rec.Init();
                Rec."User Security ID" := UsersInPlans.User_Security_ID;
                Rec."User Name" := UsersInPlans.User_Name;
                Rec."Plan ID" := UsersInPlans.Plan_ID;
                Rec."Plan Name" := UsersInPlans.Plan_Name;
                Rec.State := UsersInPlans.User_State;
                Rec.Insert();
            end;
            UsersInPlans.Close();
            Rec.FindFirst();
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL