Dynamics 365 Business Central: Switching a page to non-editable in real time via a field value

Dynamics 365 Business Central

Hi, Readers.
I saw an interesting question on the Business Central forum yesterday.
(+) Switching a page to non-editable – Dynamics 365 Business Central Forum Community Forum

He wants to switch a page to non-editable via a field value like pressing the pencil (or crayon) icon.

A few months ago, We have discussed the page mode that the page was opened in (Editable of the page), you can easily control the editable mode that page was opened in by user permissions. But this time it is slightly different, it is not controlled when the page is opened, but when the field is validated.

Well, let’s make a try.

1. Add a new field ‘Locked‘ to the Customer table and display it on the Customer Card page.

2. When Locked is changed to true, switch the Customer Card page to non-editable immediately.

3. If reopen the Customer Card page with Locked true, it is still non-editable.

4. Add an Unlock action to change the Customer Card page to be editable.

Test Video:

Source Code: for reference only

pageextension 50100 CustomerCardExt extends "Customer Card"
{
    layout
    {
        addlast(General)
        {
            field(Locked; Rec.Locked)
            {
                ApplicationArea = All;

                trigger OnValidate()
                var
                    CustomerCard: Page "Customer Card";
                begin
                    if Rec.Locked then begin
                        CustomerCard.SetRecord(Rec);
                        CurrPage.Close();
                        CustomerCard.Editable := false;
                        CustomerCard.Run();
                    end;
                end;
            }
        }
    }

    actions
    {
        addbefore(Dimensions)
        {
            action(Unlock)
            {
                Caption = 'Unlock';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Category9;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    CustomerCard: Page "Customer Card";
                begin
                    if Rec.Locked then begin
                        Rec.Locked := false;
                        Rec.Modify();
                        CustomerCard.SetRecord(Rec);
                        CurrPage.Close();
                        CustomerCard.Editable := true;
                        CustomerCard.Run();
                    end;
                end;
            }
        }
    }

    trigger OnOpenPage();
    begin
        if Rec.Locked then
            CurrPage.Editable(false);
    end;
}

tableextension 50100 CustomerExt extends Customer
{
    fields
    {
        field(50100; Locked; Boolean)
        {
            Caption = 'Locked';
            DataClassification = CustomerContent;
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL