Search code examples
dynamics-business-centraldynamics-al

How can i hide a field in Microsoft Business Central AL Programming


I want to hide the field "Other Reason" if the field "Reason" has a value of "Other". See the following page extension to see the page fields.

pageextension 50101 CJ_PurchaseReturnOrder extends "Purchase Return Order"
{
    layout
    {
        addlast(General)
        {
            field("TO Code"; Rec."TO Code")
            {
                ApplicationArea = all;
                TableRelation = "Transfer Header";
            }
            field("Item Journal Code"; Rec."Item Journal Code")
            {
                ApplicationArea = all;
                TableRelation = "Item Journal Line";
            }
            field("Reason"; Rec.Reason)
            {
                ApplicationArea = all;
            }
            field("Other Reason"; Rec."Other Reason")
            {
                ApplicationArea = all;
            }
        }
    }
}

I tried to solve this by the following code:

pageextension 50101 CJ_PurchaseReturnOrder extends "Purchase Return Order"
{
    layout
    {
        addlast(General)
        {
            field("TO Code"; Rec."TO Code")
            {
                ApplicationArea = all;
                TableRelation = "Transfer Header";
            }
            field("Item Journal Code"; Rec."Item Journal Code")
            {
                ApplicationArea = all;
                TableRelation = "Item Journal Line";
            }
            field("Reason"; Rec.Reason)
            {
                ApplicationArea = all;
            }
            field("Other Reason"; Rec."Other Reason")
            {
                ApplicationArea = all;
                Enabled = EnableToCode;
            }
        }
    }
    var
        EnableToCode: Boolean;

    trigger OnAfterGetRecord()
    begin
        if Rec.Reason = Rec.Reason::Other then
            EnableToCode := true
        else
            EnableToCode := false;
    end;
}

I am expecting the field to hide when the field of the current record is "Rec.Reason::Other". What am I missing to hide the field? Thanks in advance.


Solution

  • you almost have the solution, because you only need to use the correct property: Visible (https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-visible-property)

    pageextension 50101 CJ_PurchaseReturnOrder extends "Purchase Return Order"
    {
        layout
        {
            addlast(General)
            {
                field("TO Code"; Rec."TO Code")
                {
                    ApplicationArea = all;
                    TableRelation = "Transfer Header";
                }
                field("Item Journal Code"; Rec."Item Journal Code")
                {
                    ApplicationArea = all;
                    TableRelation = "Item Journal Line";
                }
                field("Reason"; Rec.Reason)
                {
                    ApplicationArea = all;
                }
                field("Other Reason"; Rec."Other Reason")
                {
                    ApplicationArea = all;
                    Visible = OtherReasonIsVisible;
                }
            }
        }
    
        var
            OtherReasonIsVisible: Boolean;
    
        trigger OnAfterGetRecord()
        begin
            if Rec.Reason = Rec.Reason::Other then
                OtherReasonIsVisible := true
            else
                OtherReasonIsVisible := false;
        end;
    }
    

    There is an additional information that is often forgotten, so I want to mention it here: if you are trying to set the visibility of a page field dynamically, you need to move the field into a group and use the visible property of the group to show/hide the page field dynamically (see: Dynamic visibility of controls)