Dynamics 365 Business Central: Populate/Flow Order No. & Order Line No. when using the Get Shipment Lines action in Sales Invoice

Dynamics 365 Business Central

Hi, Readers.
Last week we discussed Dynamics 365 Business Central: Populate/Flow Order No. & Order Line No. when using the Get Receipt Lines action in Purchase Invoice.

Today I would like to briefly discuss the situation with sales documents. The situation for sales is more complicated than that for purchases because the standard table 37 “Sales Line” does not have the Order No. and Order Line No. fields found in the Purchase Line.

In table 39 “Purchase Line”:

In table 37 “Sales Line”:

But the table Sales Invoice Line (113) after posting has these two fields and the field id is the same as in the purchase line. (I suspect Microsoft forgot to add these two fields to table 37 “Sales Line”…..). And, there is still one shortcoming. Even if you post directly from the Sales Order, the Order No. and Order Line No. fields will not be automatically filled.

PS: My Test Version: W1 25.3 (Platform 25.2.29617.0 + Application 25.3.28755.29759)

Let’s see more details.
As you might know, if you want to invoice more than one sales shipment at a time, you can select multiple shipment lines on the sales invoice.
Before you can create a combined sales shipment, more than one shipment from the same customer in the same currency must be posted. In other words, you must have filled in two or more sales orders and posted them as shipped, but not invoiced.

Then create a new sales invoice for the customer , on the Lines FastTab, choose the Get Shipment Lines action.

You can select multiple receipt lines that you want to include in the invoice.

In the Shipment line, you can find the Order No./Order Line No.
Order No.: 101009
Order Line No.: 10000

Unlike the purchasing situation, if we need to automatically populated Order No./Order Line No. to the Sales Line on the Sales Invoice Subform (47, ListPart). we need to start by adding fields.

PS: To prevent Microsoft from adding standard fields in the future, please add prefixes and suffixes when adding new table fields. And it is also possible to use namespace.

Then let’s look at the standard code, and find out which event we can subscribe to. (You can skip this step if you just want to see the results)
page 47 “Sales Invoice Subform”:

codeunit 64 “Sales-Get Shipment”:

page 5708 “Get Shipment Lines”:

codeunit 64 “Sales-Get Shipment”: Found it, we can use the following event.
OnAfterInsertLine(SalesShptLine, SalesLine, SalesShptLine2, TransferLine, SalesHeader);

Well. We can add a setting in Sales & Receivables Setup to control this behavior.

Then add the logic needed this time.

Great.

If we want Order No./Order Line No. to be transferred to table Sales Invoice Line (113) after posting, one more customization step is required because the IDs of custom fields and standard fields are different.

No problem.

Test video:

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)

tableextension 50112 SalesLineExt extends "Sales Line"
{
    fields
    {
        field(50065; "ZY Order No."; Code[20])
        {
            Caption = 'Order No.';
        }
        field(50066; "ZY Order Line No."; Integer)
        {
            Caption = 'Order Line No.';
        }
    }
}

pageextension 50112 SalesInvoiceSubformExt extends "Sales Invoice Subform"
{
    layout
    {
        addafter(Description)
        {
            field("ZY Order No."; Rec."ZY Order No.")
            {
                ApplicationArea = All;
                Caption = 'Order No.';
                Editable = false;
            }
            field("ZY Order Line No."; Rec."ZY Order Line No.")
            {
                ApplicationArea = All;
                Caption = 'Order Line No.';
                Editable = false;
            }
        }
    }
}

tableextension 50100 SalesReceivablesSetupExt extends "Sales & Receivables Setup"
{
    fields
    {
        field(50200; "Copy Order No. to Invoice"; Boolean)
        {
            Caption = 'Copy Order No. to Invoice';
            ToolTip = 'When enabled, the Order No. and Order Line No. fields will be copied from the Sales Order to the Sales Invoice when using the Get Shipment Lines action in Sales Invoices.';
        }
    }
}

pageextension 50100 SalesReceivablesSetupExt extends "Sales & Receivables Setup"
{
    layout
    {
        addbefore("Prepmt. Auto Update Frequency")
        {
            field("Copy Order No. to Invoice"; Rec."Copy Order No. to Invoice")
            {
                ApplicationArea = All;
            }
        }
    }
}

codeunit 50112 EventHandler
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Get Shipment", OnAfterInsertLine, '', false, false)]
    local procedure "Sales-Get Shipment_OnAfterInsertLine"(var SalesShptLine: Record "Sales Shipment Line"; var SalesLine: Record "Sales Line")
    var
        SalesReceivablesSetup: Record "Sales & Receivables Setup";
    begin
        if SalesReceivablesSetup.Get() then
            if SalesReceivablesSetup."Copy Order No. to Invoice" then begin
                SalesLine."ZY Order No." := SalesShptLine."Order No.";
                SalesLine."ZY Order Line No." := SalesShptLine."Order Line No.";
                SalesLine.Modify(true);
            end;
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnBeforeSalesInvLineInsert, '', false, false)]
    local procedure "Sales-Post_OnBeforeSalesInvLineInsert"(var SalesInvLine: Record "Sales Invoice Line"; SalesLine: Record "Sales Line")
    var
        SalesReceivablesSetup: Record "Sales & Receivables Setup";
    begin
        if SalesReceivablesSetup.Get() then
            if SalesReceivablesSetup."Copy Order No. to Invoice" then begin
                SalesInvLine."Order No." := SalesLine."ZY Order No.";
                SalesInvLine."Order Line No." := SalesLine."ZY Order Line No.";
            end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL