Use the InitRecord function
Most documents and their corresponding tables and pages include several functions that are used often. For example, after the number series is initialized in the document header tables, many other fields are initialized to default values according to the business process requirements for the document. All such code is put into the InitRecord function that is called immediately after the number series is initialized.
The following example shows a snippet of the implementation of the InitRecord function in the Sales Header table. This function configures and initializes fields in the Sales Header table, and also sets dates such as the Posting Date, Order Date, and Document Date.
/// <summary>
/// Initializes a new sales header with default values.
/// </summary>
procedure InitRecord()
var
ShipToAddress: Record "Ship-to Address";
ArchiveManagement: Codeunit ArchiveManagement;
LocationCode: Code[10];
IsHandled: Boolean;
NewOrderDate: Date;
begin
GetSalesSetup();
IsHandled := false;
OnBeforeInitRecord(Rec, IsHandled, xRec);
if not IsHandled then
InitPostingNoSeries();
InitShipmentDate();
InitPostingDate();
if SalesSetup."Default Posting Date" = SalesSetup."Default Posting Date"::"No Date" then
"Posting Date" := 0D;
NewOrderDate := WorkDate();
OnInitRecordOnBeforeAssignOrderDate(Rec, NewOrderDate);
"Order Date" := NewOrderDate;
"Document Date" := WorkDate();
if "Document Type" = "Document Type"::Quote then
CalcQuoteValidUntilDate();
if "Sell-to Customer No." <> '' then
GetCust("Sell-to Customer No.");
LocationCode := Customer."Location Code";
AltCustVATRegFacade.Init(Rec, xRec);
if "Ship-to Code" <> '' then begin
ShipToAddress.SetLoadFields("Location Code");
if ShipToAddress.Get("Sell-to Customer No.", "Ship-to Code") then
if ShipToAddress."Location Code" <> '' then
LocationCode := ShipToAddress."Location Code";
end;
UpdateLocationCode(LocationCode);
if IsCreditDocType() then begin
GLSetup.Get();
Correction := GLSetup."Mark Cr. Memos as Corrections";
end;
InitVATDate();
InitPostingDescription();
UpdateOutboundWhseHandlingTime();
IsHandled := false;
OnInitRecordOnBeforeAssignResponsibilityCenter(Rec, IsHandled);
if not IsHandled then
"Responsibility Center" := UserSetupMgt.GetRespCenter(0, "Responsibility Center");
IsHandled := false;
OnInitRecordOnBeforeGetNextArchiveDocOccurrenceNo(Rec, IsHandled);
if not IsHandled then
"Doc. No. Occurrence" := ArchiveManagement.GetNextOccurrenceNo(DATABASE::"Sales Header", Rec."Document Type".AsInteger(), Rec."No.");
OnAfterInitRecord(Rec);
end;