How to change field captions dynamically in Dynamics 365 Business Central

Dynamics 365 Business Central

Hello Everyone.
Today, a typhoon is approaching in Kanto, Japan. Fortunately, it deviated slightly and headed northeast. Traffic etc. were not affected.
Let’s go back to the title, this time I want to discuss how to change field captions dynamically in Dynamics 365 Business Central.
For example, when you enter a field, the caption of other fields on the page changes.
On why this topic came to mind, because my previous job required to make a weekly report in Excel to simply record contents and times of the work. The more important point here is enter the date of Monday, and the column names automatically show all the dates of this week. As we know, It is easier to implement in Excel. But can it be achieved in Business Central?
The answer is yes.
You can first check the video below.

Let’s try it step by step.
First I created a new weekly report table.
It contains, Line No (Key), 7 days of the week, work content (Very simple), First Day.

table 50149 WeeklyReport
{
    DataClassification = CustomerContent;
    Caption = 'Weekly Report';

    fields
    {
        field(1; LineNo; Integer)
        {
            Caption = 'Line No.';
            DataClassification = CustomerContent;
        }
        field(10; Day01; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(20; Day02; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(30; Day03; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(40; Day04; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(50; Day05; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(60; Day06; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(70; Day07; Decimal)
        {
            DataClassification = CustomerContent;
        }
        field(80; Workcontent; Text[50])
        {
            Caption = 'Work Content';
            DataClassification = CustomerContent;
        }
        field(90; FirstDay; Date)
        {
            Caption = 'The First Day';
            DataClassification = CustomerContent;
        }
    }

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

Then create a new Worksheetpage to to show the fields.

As following.

About the caption of the fields.
We can use the CaptionClass Property.
CaptionClass Property

For example:
CaptionClass = ‘1,5,,’ + DayCaption01;

Some Explanation:
The first parameter says it is a dimension
The second parameter says to use a certain format (= optional parameter 1 + dimension caption + optional parameter 2) of the dimension-code (not value-code!) put in parameter 3.
The third parameter is the dimension-code you want. This parameter must contain a value that does not exist in the dimensions (a blank for example). If this dimension does not exists, codeunit 1 shows the fourth parameter on the caption.
The fourth parameter : the value you want to show as caption.

https://forum.mibuso.com/discussion/6746/customized-captions-at-run-time-without-changing-codeunit-1

Function to set the captions.

The function is executed when “FirstDay” is validated or when OnAfterGetRecord trigger is run on the page.
There is also a control that “Firstday” can only enter the date of Monday.

Source code:

page 50149 WorkingTimeRecords
{
    Caption = 'Working Time Records';
    PageType = Worksheet;
    SourceTable = WeeklyReport;
    ApplicationArea = All;
    UsageCategory = Lists;
    AutoSplitKey = true;
    DelayedInsert = true;

    layout
    {
        area(content)
        {
            field(FirstDay; FirstDay)
            {
                ApplicationArea = All;

                trigger OnValidate()
                var
                begin
                    if (FirstDay <> 0D) and (DATE2DWY(FirstDay, 1) <> 1) then
                        Error('You can only enter the date of Monday.');
                    SetDate();
                end;
            }
            repeater(General)
            {
                field(Workcontent; Workcontent)
                {
                    Caption = 'Work Content';
                    ApplicationArea = All;
                }
                field(Day01; Day01)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption01;
                }
                field(Day02; Day02)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption02;
                }
                field(Day03; Day03)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption03;
                }
                field(Day04; Day04)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption04;
                }
                field(Day05; Day05)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption05;
                }
                field(Day06; Day06)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption06;
                }
                field(Day07; Day07)
                {
                    ApplicationArea = All;
                    CaptionClass = '1,5,,' + DayCaption07;
                }
            }
        }
    }

    trigger OnAfterGetRecord()
    var
    begin
        SetDate();
    end;

    local procedure SetDate()
    var
    begin
        Clear(DayCaption01);
        Clear(DayCaption02);
        Clear(DayCaption03);
        Clear(DayCaption04);
        Clear(DayCaption05);
        Clear(DayCaption06);
        Clear(DayCaption07);
        if FirstDay <> 0D then begin
            DayCaption01 := Format(FirstDay) + ' (Monday)';
            DayCaption02 := Format(CalcDate('<+1D>', FirstDay)) + ' (Tuesday)';
            DayCaption03 := Format(CalcDate('<+2D>', FirstDay)) + ' (Wednesday)';
            DayCaption04 := Format(CalcDate('<+3D>', FirstDay)) + ' (Thursday)';
            DayCaption05 := Format(CalcDate('<+4D>', FirstDay)) + ' (Friday)';
            DayCaption06 := Format(CalcDate('<+5D>', FirstDay)) + ' (Saturday)';
            DayCaption07 := Format(CalcDate('<+6D>', FirstDay)) + ' (Sunday)';
        end;
        CurrPage.Update();
    end;

    var
        DayCaption01, DayCaption02, DayCaption03, DayCaption04, DayCaption05, DayCaption06, DayCaption07 : Text[50];
}

Let’s publish it to Business Central.

Open this page.

Enter a date (Monday)
For Example: 2020/09/21

Save.
Caption has changed.

Enter another Monday.
For Example: 2020/09/28

If the entered date is not Monday. The error message will be shown.
For Example: 2020/09/29

END

Hope this will help.
Thanks.

コメント

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