Dynamics 365 Business Central: User Lookup (Using codeunit 9843 “User Selection”)

Dynamics 365 Business Central

Hi, Readers.
Recently I saw a question on the Dynamics 365 Business Central Forum about how to get user list and return the User Name selected by the user, How to get user list and select range of users.

In this post, I would like to briefly talk about User Lookup in Business Central.

Let’s assume we need to add a user name field to a customer table to manage the user who are primarily responsible for that customer. The user needs to be selected from the current user table.
This is not very difficult, the following two methods are commonly used.

1. Using TableRelation Property and ValidateTableRelation Property

You can find a reference in User Setup page.

For example:

Test Video:

Source Code:

pageextension 50001 ZYCustomerCardExt extends "Customer Card"
{
    layout
    {
        addbefore(Blocked)
        {
            field("User Name"; Rec."User Name")
            {
                ApplicationArea = All;
            }
        }
    }
}

tableextension 50001 ZYCustomerExt extends Customer
{
    fields
    {
        field(50100; "User Name"; Code[50])
        {
            Caption = 'User Name';
            DataClassification = CustomerContent;
            TableRelation = User."User Name";
            ValidateTableRelation = false;
        }
    }
}

2. Using OnLookup (Field) Trigger and Page.RunModal() Method (page 9843 “User Lookup”)

This method is somewhat different from TableRelation in user operation, requiring you to select the data you need in a new pop-up page.

For example:

Test Video:

Source Code:

pageextension 50001 ZYCustomerCardExt extends "Customer Card"
{
    layout
    {
        addbefore(Blocked)
        {
            field("User Name"; Rec."User Name")
            {
                ApplicationArea = All;
            }
        }
    }
}

tableextension 50001 ZYCustomerExt extends Customer
{
    fields
    {
        field(50100; "User Name"; Code[50])
        {
            Caption = 'User Name';
            DataClassification = CustomerContent;

            trigger OnLookup()
            var
                User: Record User;
            begin
                User.Reset();
                if Page.RunModal(Page::"User Lookup", User) = Action::LookupOK then
                    "User Name" := User."User Name";
            end;
        }
    }
}

3. Using User Selection Module (codeunit 9843 “User Selection”)

User Selection Module: Provides a page where you look up and select one or more registered users. For example, this is useful for assigning a person to things like documents, processes, or items.

This method is similar to method 2, but it uses the standard module. Let’s update the code.

Open (Method): Opens the user lookup page and assigns the selected users on the parameter.

For example:

Source Code:

pageextension 50001 ZYCustomerCardExt extends "Customer Card"
{
    layout
    {
        addbefore(Blocked)
        {
            field("User Name"; Rec."User Name")
            {
                ApplicationArea = All;
            }
        }
    }
}

tableextension 50001 ZYCustomerExt extends Customer
{
    fields
    {
        field(50100; "User Name"; Code[50])
        {
            Caption = 'User Name';
            DataClassification = CustomerContent;

            trigger OnLookup()
            var
                User: Record User;
                UserSelection: Codeunit "User Selection";
            begin
                User.Reset();
                UserSelection.Open(User);
                "User Name" := User."User Name";
            end;
        }
    }
}

PS:
1. ValidateUserName (Method): Displays an error if there is no user with the given username and the user table is not empty.
So we can do like the following.

Test Video:

2. HideExternalUsers (Method): Sets Filter on the given User Record to exclude external users. (License Type = External User)

If you use page 9843 “User Lookup”, this method has been called when the page is opened, so you don’t need to use it again.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL