Dynamics 365 Business Central: Date-Time Dialog

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about the best practice for entering DateTime values in Dynamics 365 Business Central.

First, let’s look at what the DateTime Data Type is.

DateTime Data Type: Denotes a date and time ranging from January 1, 1753, 00:00:00.000 to December 31, 9999, 23:59:59.999. An undefined or blank DateTime is specified by 0DT.

The displayed text format of a DateTime is determined by your Regional and Language Options in Windows.

For example,
Region: English (United States)
Language: English (United States)

I added a new field “Earliest Contact Date/Time” to the Customer table and displayed it on the Customer Card page.

When users enter a value on the page, they need to enter the date and time in the same box.
If the users forget to enter the time, the time will be defaulted to 00:00 (000000T) and every time the users reselect the date, the time will be cleared.

Of course, this is not terribly inconvenient, but it is not the best practice.

In Business Central, Microsoft has given us an out-of-the-box Date-Time Dialog. Using it can improve the user experience on the UI.
page 684 “Date-Time Dialog”: Dialog for entering DateTime values.

Let us improve the above code.

Test Video:

Source Code:

tableextension 50100 ZYCustomerExt extends Customer
{
    fields
    {
        field(50100; "Earliest Contact Date/Time"; DateTime)
        {
            Caption = 'Earliest Contact Date/Time';
            DataClassification = CustomerContent;

            trigger OnLookup()
            begin
                Validate("Earliest Contact Date/Time", LookupDateTime("Earliest Contact Date/Time"));
            end;
        }
    }

    procedure LookupDateTime(InitialValue: DateTime): DateTime
    var
        DateTimeDialog: Page "Date-Time Dialog";
        NewValue: DateTime;
    begin
        DateTimeDialog.SetDateTime(InitialValue);

        if DateTimeDialog.RunModal() = Action::OK then
            NewValue := DateTimeDialog.GetDateTime();

        exit(NewValue);
    end;
}

pageextension 50100 ZYCustomerCardExt extends "Customer Card"
{
    layout
    {
        addafter(Blocked)
        {
            field("Earliest Contact Date/Time"; Rec."Earliest Contact Date/Time")
            {
                Caption = 'Earliest Contact Date/Time';
                ApplicationArea = All;
            }
        }
    }
}

PS: Where to find references from?
1. There is a very good example on the Date-Time Dialog website.

2. On the Job Queue Entry Card page, you can refer to the code in the field “Earliest Start Date/Time“.

Give it a try.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL