Dynamics 365 Business Central: How to find out available Roles/Profiles via AL (About the Available Roles/Profiles Lookup)

Dynamics 365 Business Central

Hi, Readers.
Yesterday, we briefly discussed Allow user selection of Role Center (Profile) for each company (Multiple roles for one user). There is one point that I would like to save for today to explain in detail, how to find out available Roles/Profiles via AL (About the Available Roles/Profiles Lookup).

I was asked this question before. A developer added a field to record the user Role/Profile in their customization, but the field values did not show the same contents as the standard one when opened.

For example, lookup from My Settings page:

There are currently 14 available roles in the standard environment.

Page: Roles (9212, List)
Table: All Profile (2000000178) – The source table of this page is temporary.

As you might know, these values can be found in the in Profile List (9171, List) page.
All Profile (2000000178):

So the general approach is to use the TableReleation Property, which can simply link the All Profile (2000000178) table.

But at this time, you will find that the displayed content, including the number of roles, is completely different from the standard one.

You may also find the Show in Role Explorer field on the Profiles (Roles) page, but the numbers still don’t match.

So how to deal with it? In fact, as I mentioned briefly in How to bulk edit user roles/profiles (Customization), there is standard processing here.

Let’s take a quick look at the standard logic.

page 9204 “User Settings”:

codeunit 9175 “User Settings Impl.”: Access = Internal;

The key point this time:

But the Access property of codeunit 9175 “User Settings Impl.” is Internal (The object can be accessed only by code in the same module, but not from another module), so we can’t refer to it directly.

But we can copy these standard codes to replicate what it does.
For example,

Test Video:

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs and for the convenience of copying, I have concentrated the code in an AL file, please manage it separately in your project.)

table 50101 "ZY Users Profiles"
{
    DataClassification = CustomerContent;

    fields
    {
        field(1; UserID; Code[20])
        {
            Caption = 'User ID';
            TableRelation = User."User Name";
            ValidateTableRelation = false;
            DataClassification = CustomerContent;
        }
        field(2; ProfileID; Code[30])
        {
            Caption = 'Profile ID';
            DataClassification = CustomerContent;

            trigger OnLookup()
            var
                TempAllProfile: Record "All Profile" temporary;
            begin
                PopulateProfiles(TempAllProfile);
                if Page.RunModal(Page::Roles, TempAllProfile) = Action::LookupOK then begin
                    ProfileID := TempAllProfile."Profile ID";
                end;
            end;
        }
    }

    keys
    {
        key(PK; UserID)
        {
            Clustered = true;
        }
    }

    var
        DescriptionFilterTxt: Label 'Navigation menu only.';
        UserCreatedAppNameTxt: Label '(User-created)';

    local procedure PopulateProfiles(var TempAllProfile: Record "All Profile" temporary)
    var
        AllProfile: Record "All Profile";
    begin
        TempAllProfile.Reset();
        TempAllProfile.DeleteAll();
        AllProfile.SetRange(Enabled, true);
        AllProfile.SetFilter(Description, '<> %1', DescriptionFilterTxt);
        if AllProfile.FindSet() then
            repeat
                TempAllProfile := AllProfile;
                if IsNullGuid(TempAllProfile."App ID") then
                    TempAllProfile."App Name" := UserCreatedAppNameTxt;
                TempAllProfile.Insert();
            until AllProfile.Next() = 0;
        TempAllProfile.FindFirst();
    end;
}

page 50103 "ZY Users Profiles"
{
    ApplicationArea = All;
    Caption = 'ZY Users Roles';
    PageType = List;
    SourceTable = "ZY Users Profiles";
    UsageCategory = Lists;
    DelayedInsert = true;

    layout
    {
        area(content)
        {
            repeater(General)
            {
                field(UserID; Rec.UserID)
                {
                    ToolTip = 'Specifies the value of the User ID field.';
                }
                field(ProfileID; Rec.ProfileID)
                {
                    ToolTip = 'Specifies the value of the Profile ID field.';
                }
            }
        }
    }
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL