Dynamics 365 Business Central: How to get the opening Page ID and Page name

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to get the opening Page ID and Page name in Business Central.

I was asked this question a few days ago, they want to determine if the user has permission to open the page based on the page ID. It’s actually not very difficult. This time we can use Page.ObjectId([Boolean]) Method.

Page.ObjectId([Boolean]) Method: Returns a string in the “Page xxx” format, where xxx is the caption or ID of the application object.

true returns the page caption, if there is one; otherwise, it returns the page name. false returns the page ID as text.

Let’s see a simple example.

We can update the code to keep only the ID and Name.

So for the above permission requirement, we can do a small customization.

Source Code:

pageextension 50103 CustomerExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        UserPagePermission: Record UserPagePermission;
        PageID: Text;
    begin
        PageID := CopyStr(CurrPage.ObjectId(false), 6);
        if not UserPagePermission.Get(UserId, PageID) then
            Error('You do not have permission to open the page! Please check User Page Permissions settings.');
    end;
}

table 50105 UserPagePermission
{
    DataClassification = CustomerContent;

    fields
    {
        field(1; UserID; Code[50])
        {
            DataClassification = CustomerContent;
            TableRelation = "User Setup"."User ID";
        }
        field(2; PageID; Integer)
        {
            DataClassification = CustomerContent;
            TableRelation = AllObjWithCaption."Object ID" where("Object Type" = filter(Page));
        }
    }

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

page 50103 UserPagePermissions
{
    Caption = 'User Page Permissions';
    PageType = List;
    ApplicationArea = All;
    UsageCategory = Administration;
    SourceTable = UserPagePermission;

    layout
    {
        area(Content)
        {
            repeater(Control1)
            {
                field(UserID; Rec.UserID)
                {
                    Caption = 'User ID';
                    ToolTip = 'Specifies the value of the UserName field.';
                    ApplicationArea = All;
                }
                field(PageID; Rec.PageID)
                {
                    Caption = 'Page ID';
                    ToolTip = 'Specifies the value of the PageID field.';
                    ApplicationArea = All;
                }
            }
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

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