Dynamics 365 Business Central: How to set default work date? – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how to set default work date in Business Central.

As you might know you can use a work date to specify a date that isn’t today’s date on records. For example, a work date is useful when you need to set a particular date for multiple records. You specify the work date on the My Settings page.

After you change the work date, if you sign out or switch to another company, the work data reverts to the default work date. So the next time you sign in or switch back to the original company, you may have to set the work date again.

If you haven’t specified a work date, today’s date will be used. But for the trial environment, the Work Date will be fixed on a special date, such as 2023.04.10.

PS:
1. The most commonly used work date is today’s date. You may have to temporarily change the work date to be able to perform tasks, such as completing transactions for a date that is not today. If the work date in the system is different from today’s date, there will be a reminder appears at the top of the page.

Notifies the user that the work date in the system is different from today’s date and provides a link to change the work date.

More details: Work Date

2. In all date fields, type t to quickly enter today’s date, and type w to quickly enter the work date, which is the value in the Work Date field on the My Settings page.

3. After you change the work date, if you sign out or switch to another company, the work data reverts to the default work date. So the next time you sign in or switch back to the original company, you may have to set the work date again.

So can we set the default work date in Business Central? For example, some companies want the work date to be set to the end of the current month every time (perhaps this is a very special requirement). Or in a trial environment, the tester wants the work date to be set to today’s date. Is this possible? Yes. We need to use two points for this.

First, to set the work date, we can use the following method.

System.WorkDate([Date]) Method: Gets and sets the work date for the current session.

[WorkDate := ]  System.WorkDate([NewDate: Date])

As for the subscribed event, we can use the OnAfterLogin event mentioned in the example below. More details: Setting the Signup Context value in the Signup Context Values table

Let’s look at a simple example.
First add a new field in General Ledger Setup to manage the default Work Date. (When updating the work date (new field), the current Work Date will not be changed.)

After the user logs in, execute the following logic. If the default Work Date is not set in General Ledger Setup, Work Date will be automatically set to today’s date. If it is set, Work Date will be set to the set date.

Looks good.

Test Video:

Great, give it a try!!!😁

Source Code: GitHub (Please note that the source code is for reference only, you can improve it according to your own needs)

codeunit 50117 WorkDateHandle
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"System Initialization", OnAfterLogin, '', false, false)]
    local procedure "System Initialization_OnAfterLogin"();
    var
        GeneralLedgerSetup: Record "General Ledger Setup";
    begin
        if GeneralLedgerSetup.Get() then
            if GeneralLedgerSetup.DefaultWorkDate <> 0D then
                WorkDate(GeneralLedgerSetup.DefaultWorkDate)
            else
                WorkDate(Today);
    end;
}

tableextension 50117 GeneralLedgerSetupExt extends "General Ledger Setup"
{
    fields
    {
        field(50100; DefaultWorkDate; Date)
        {
            Caption = 'Default Work Date';
            DataClassification = CustomerContent;
        }
    }
}

pageextension 50117 GeneralLedgerSetupExt extends "General Ledger Setup"
{
    layout
    {
        addafter("Allow Posting To")
        {
            field(DefaultWorkDate; Rec.DefaultWorkDate)
            {
                ApplicationArea = All;
                Caption = 'Default Work Date';
            }

        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL