Dynamics 365 Business Central: Extending Default Posting Date (Today, Beginning of the month, End of the month)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about a question I was asked recently, can we extend Default Posting Date in Business Central.
As you might know, in Sales & Receivables Setup and Purchases & Payables Setup we can set the Default Posting Date for sales and purchase documents, Work Date or No Date.

Sales & Receivables Setup:

Default Posting Date
Specifies which date must be used as the default posting date on sales documents. If you select Work Date, the Posting Date field will be populated with the work date at the time of creating a new sales document. If you select No Date, the Posting Date field will be empty by default and you must manually enter a posting date before posting.

Purchases & Payables Setup

Default Posting Date
Specifies which date must be used as the default posting date on purchase documents. If you select Work Date, the Posting Date field will be populated with the work date at the time of creating a new purchase document. If you select No Date, the Posting Date field will be empty by default and you must manually enter a posting date before posting.

For example, Work Date

No Date:

So if we want to extend the Default Posting Date, can we, for example add Today, Beginning of the month, End of the month? Yes, of course. First let’s check the standard code.

table 311 “Sales & Receivables Setup”:

table 312 “Purchases & Payables Setup”:

enum 35 “Default Posting Date”:

table 36 “Sales Header”:

InitPostingDate():

table 38 “Purchase Header”:

Let’s do a simple test.
First, enums can be extended in order to add more values to the enumeration list.

Then we need to subscribe to the following events.

Database::”Sales Header”, OnInitRecordOnBeforeAssignOrderDate
Database::”Purchase Header”, OnInitRecordOnAfterAssignDates

Done.
Test video for sales documents:

Very simple, 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)

enumextension 50100 DefaultPostingDateExt extends "Default Posting Date"
{
    value(50000; Today)
    {
    }
    value(50001; "Beginning of the month")
    {
    }
    value(50002; "End of the month")
    {
    }
}

codeunit 50111 DefaultPostingDateHandler
{
    [EventSubscriber(ObjectType::Table, Database::"Sales Header", OnInitRecordOnBeforeAssignOrderDate, '', false, false)]
    local procedure OnInitRecordOnBeforeAssignOrderDate(var SalesHeader: Record "Sales Header"; var NewOrderDate: Date)
    var
        SalesSetup: Record "Sales & Receivables Setup";
    begin
        SalesSetup.Get();
        case SalesSetup."Default Posting Date" of
            SalesSetup."Default Posting Date"::Today:
                SalesHeader."Posting Date" := Today;
            SalesSetup."Default Posting Date"::"Beginning of the month":
                SalesHeader."Posting Date" := CalcDate('<-CM>', Today);
            SalesSetup."Default Posting Date"::"End of the month":
                SalesHeader."Posting Date" := CalcDate('<CM>', Today);
        end;
    end;

    [EventSubscriber(ObjectType::Table, Database::"Purchase Header", OnInitRecordOnAfterAssignDates, '', false, false)]
    local procedure OnInitRecordOnAfterAssignDates(var PurchaseHeader: Record "Purchase Header")
    var
        PurchSetup: Record "Purchases & Payables Setup";
    begin
        PurchSetup.Get();
        case PurchSetup."Default Posting Date" of
            PurchSetup."Default Posting Date"::Today:
                PurchaseHeader."Posting Date" := Today;
            PurchSetup."Default Posting Date"::"Beginning of the month":
                PurchaseHeader."Posting Date" := CalcDate('<-CM>', Today);
            PurchSetup."Default Posting Date"::"End of the month":
                PurchaseHeader."Posting Date" := CalcDate('<CM>', Today);
        end;
    end;
}

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

2. Dynamics 365 Business Central: How to get Start Date and End Date (Period) of the week/month/quarter/year (Includes last and next) from a given date

3. Dynamics 365 Business Central: How to quickly check whether the posting date is valid in AL (Within the allowed posting date range)

4. Business Central 2023 wave 2 (BC23): Sync document and posting dates for sales and purchases (New ‘Link Doc. Date to Posting Date’ setting)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL