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
END
Hope this will help.
Thanks for reading.
ZHU
コメント