Dynamics 365 Business Central: show/hide fields on pages

Showing and hiding fields on pages based on conditions seems to be a very easy task for a classic NAV developer, but with Dynamics 365 Business Central and the AL extensions I see very very often that this is a source of hours spent on calling all the saints in paradise.

In order to avoid disturbing the saints, I’ll try to describe how to do that in Dynamics 365 Business Central with a simple example.

I’ve created a small extension that adds a Book table, a Book List page and a Book Card page. A Book record has an “Hide ISBN” field that, when set to true, it must hide the ISBN field. This field must be visible if the “Hide ISBN” field is false.

The Book table is defined as follows:

table 50110 Book

{

  DataClassification = CustomerContent;

  LookupPageId = BookList;

  DrillDownPageId = BookList;

  fields

  {
    field(1; ID; Code[20])
    {
      DataClassification = CustomerContent;
    }

    field(2; Description; Text[50])
    {
      DataClassification = CustomerContent;
    }

    field(3; ISBN; Code[20])
    {
      DataClassification = CustomerContent;
    }

    field(4; Hide; Boolean)
    {
      Caption = 'Hide ISBN';
      DataClassification = CustomerContent;
    }
  }

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

Then I’ve defined the two pages (Book Card and Book List).

As first step, in the Book Card page I’ve created the logic to hide the ISBN field exactly like we’re doing now in NAV:

BCHideFields_00

Here, the Visible property of the ISBN field is bounded to the isVisible global Boolean variable defined in the Book Card page (and setted as true in the OnOpenPage trigger).

In the OnValidate trigger of the Hide field I set the isVisible value accordingly with the Hide field value (P.S. this is a quick sample so I’ve written the code directly in the page, but in the real world it’s much better to use events and avoid writing code on pages).

What happens when I deploy the extension to Dynamics 365 Business Central?

I’ve created two Book records:

BCHideFields_01

Now I open the Book Card page:

BCHideFields_02

Result: the ISBN field is not displayed, despite the value of the Hide field!

What’s the trick to have the correct show/hide behaviour? Use a group!

In the Book Card page, now I’ve created a group with ShowCaption = false and Visible = my isVisible variable and inside this group I’ve placed the field that I want to show/hide:

BCHideFields_03

If I deploy the extension again to Dynamics 365 Business Central, what happens now?

The ISBN field is visible as default when opening the Book Card page:

BCHideFields_04.jpg

When I click the Hide button, it becomes hidden:

BCHideFields_05.jpg

and this is exactly the desired result.

The complete Book Card code is as follows (if needed):

page 50111 BookCard
{
  PageType = Card;
  ApplicationArea = All;
  UsageCategory = Administration;
  SourceTable = Book;
  Caption = 'Book Card';
 
 layout
  {
    area(Content)
    {
      group(General)
      {
        field(ID; ID)
        {
          ApplicationArea = All;
        }

        field(Description; Description)
        {
          ApplicationArea = All;
        }

        field(Hide; Hide)
        {
          ApplicationArea = All;

          trigger OnValidate()
          begin
            if Hide then
              isVisible := false
            else
             isVisible := true;
          end;
        }

        group("HideGroup")
        {
          ShowCaption = false;
          Visible = isVisible;
          
          field(ISBN; ISBN)
          {
            ApplicationArea = All;
          }
        }
     }
   }
 }

 var
   isVisible: Boolean;

   trigger OnOpenPage()
   begin
     isVisible := true;
   end;
}

I think that Microsoft could improve something on this topic 🙂

 

7 Comments

  1. Thanks for the article! Really helpful. In the case you want to hide the fields in a subform, like in a sales line page for example what should you do? I guess you get an error that you cannot use groups inside a control of the type ‘repeater’…

    Like

  2. Just do as following:
    pageextension 50111 SalesInvoiceExt extends “Sales Invoice Subform”
    {
    layout
    {
    modify(“Unit Cost (LCY)”)
    {
    Visible = false;
    }
    }
    }

    Like

    1. I guess I didnt explain right. I had the same problem as you. My field visiblity is controled by a value from the header record. So in BC is always hidden. But I cant put my field inside of a group since its on a list.

      Like

  3. Hi, does the same apply on report request pages, please? In the code below, I would expect that when the DefaultTC is false, the CompanyTC should show, but it’s not working;

    requestpage
    {
    SaveValues = true;

    layout
    {
    area(content)
    {
    group(Options)
    {
    field(DefaultTc; DefaultTc)
    {
    ApplicationArea = All;
    Caption = ‘Use Default T&Cs’;

    trigger OnValidate()
    begin
    if DefaultTc then
    ShowTC := false
    else
    ShowTC := true;
    end;
    }

    group(TermsConditions)
    {
    ShowCaption = false;
    Visible = ShowTC;

    field(CompanyTC; CompanyTC)
    {
    ApplicationArea = All;
    }
    }
    }
    }
    }
    }

    Like

  4. I am trying to do something similar but on a Page Extension … I want to hide an Action that I am creating for some records.

    I can’t find a way to hide the field when the record opens or when the main value is set.

    In my case, it is the Account Schedules (Acc. Schedule Line page). I want to show an Action if the Account Schedule name is a certain value. It seems so simple but I cannot find out how to do it. Any ideas?

    Like

  5. Is it possible to hide the function new on page extension
    So that users couldn’t add new records !

    We have to personalise the page on itself permissions set are not enough in our case

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.