D365 Business Central : Adding Total to Sales Order Page

If you look at the Sales Order page, you can see that there are total fields under the Lines section. I recently had a request to add another total field to it, such as Quantity. To do this, we can easily subscribe to the Document Totals codeunit.

The Document Totals codeunit is used to calculate total on sales and purchase documents.

Let’s try to subscribe to the events.

There are two events that you need to subscribe: OnAfterCalculateSalesSubPageTotals and OnAfterSalesDeltaUpdateTotals. We are going to use the existing Quantity field to collect the Total Quantity.

codeunit 60001 "Subs Document Totals_TNG"
{
    SingleInstance = true;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document Totals", 'OnAfterSalesDeltaUpdateTotals', '', false, false)]
    local procedure DocumentTotals_OnAfterSalesDeltaUpdateTotals(var TotalSalesLine: Record "Sales Line"; var SalesLine: Record "Sales Line"; var xSalesLine: Record "Sales Line")
    begin
        TotalSalesLine.Quantity += SalesLine."Quantity" - xSalesLine."Quantity";
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document Totals", 'OnAfterCalculateSalesSubPageTotals', '', false, false)]
    local procedure DocumentTotals_OnAfterCalculateSalesSubPageTotals(var TotalSalesLine2: Record "Sales Line")
    begin
        TotalSalesLine2.CalcSums(Quantity);
    end;
}

After subscribing to the codeunit, we just need to add page extension to show the field. I am going to put it before the Total Amount Excluding VAT.

pageextension 60001 "Sales Order Subform_TNG" extends "Sales Order Subform"
{
    layout
    {
        addbefore("Total Amount Excl. VAT")
        {
            field("Total Quantity_TNG"; TotalSalesLine.Quantity)
            {
                ApplicationArea = Basic, Suite;
                DecimalPlaces = 0 : 5;
                Caption = 'Total Quantity';
                Editable = false;
                ToolTip = 'Specifies the sum of the quantity on all lines in the document.';
            }
        }
    }
}

Let’s try publishing it.

That’s it. Hope that helps.

Sample source available on Git Hub.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

5 Responses

  1. LearnBC says:

    Adding Total to Sales Order Page
    thanks for the tip. if we want to bring the same for sales invoice, purchase invoice or any other page how we can..which code unit can we use?

  2. Bryma says:

    Hi ThatNavGuy… thanks for this. Please i will like to know if I can extend the invoice page by writing the first line code like this : pageextension 60001 “Sales Invoice Subform_TNG” extends “Sales Invoice Subform”.

    Also, when I create and convert a sales order to Invoice, will the total quantity field reflect on the invoice report?

    Thanks

    • thatnavguy says:

      Hi Bryma,

      I haven’t tried it on Sales invoice, but you should be able to use the same logic for sales invoices. the customisation only for the page. if you want to show the total quantity on report, you need to customise the report.

      • Bryma says:

        I have tried extending the invoice report by adding the calculated field for total quantity, but it still not working. I just want to know what is the ideal sequence to use. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *