Use the InitRecord function

Completed

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 will configure and initialize fields in the Sales Header table, and will also set dates such as the Posting Date, Order Date, and Document Date.

procedure InitRecord()
begin
    GetSalesSetup();
    case "Document Type" of
        "Document Type"::Quote, "Document Type"::Order:
           begin
                NoSeriesMgt.SetDefaultSeries("Posting No. Series", 
SalesSetup."Posted Invoice Nos.");
                NoSeriesMgt.SetDefaultSeries("Shipping No. Series", 
SalesSetup."Posted Shipment Nos.");              
if "Document Type" = "Document Type"::Order then begin                   
NoSeriesMgt.SetDefaultSeries("Prepayment No. Series", 
SalesSetup."Posted Prepmt. Inv. Nos.");              
NoSeriesMgt.SetDefaultSeries(\"Prepmt. Cr. Memo No. Series\", \
SalesSetup.\"Posted Prepmt. Cr. Memo Nos.\")
                end;
            end;
        "Document Type"::Invoice:
            begin
                if ("No. Series" <> '') and
         (SalesSetup."Invoice Nos." = SalesSetup."Posted Invoice Nos.")
                then
                   "Posting No. Series" := "No. Series"
                else
                   NoSeriesMgt.SetDefaultSeries("Posting No. Series", 
SalesSetup."Posted Invoice Nos.");
                if SalesSetup."Shipment on Invoice" then
                    NoSeriesMgt.SetDefaultSeries("Shipping No. Series", 
SalesSetup."Posted Shipment Nos.");
            end;
    end;
...
   if SalesSetup."Default Posting Date" = 
SalesSetup."Default Posting Date"::"No Date" then
            "Posting Date" := 0D;

    "Order Date" := WorkDate();
    "Document Date" := WorkDate();
    if "Document Type" = "Document Type"::Quote then
            CalcQuoteValidUntilDate();

    UpdateLocationCode(Cust."Location Code");

    if IsCreditDocType() then begin
        GLSetup.Get();
        Correction := GLSetup."Mark Cr. Memos as Corrections";
    end;

    "Posting Description" := Format("Document Type") + ' ' + "No.";
end;