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 🙂

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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