Saturday, 8 March 2025

How to Display the Latest 10 Posted Sales Invoice Lines in a Sales Order FactBox

 

In Microsoft Dynamics 365 Business Central, users often need quick access to historical sales invoice data while working on Sales Orders. A common requirement is to display the latest 10 posted sales invoice lines related to a specific Sales Order Line. This blog post walks through how to create a FactBox that dynamically updates based on the selected Sales Order Line.


Understanding the Requirement

We need to:

1.      Create a FactBox that displays Posted Sales Invoice Lines.

2.      Link the FactBox to Sales Order Lines, ensuring that when a user selects a Sales Order Line, the FactBox updates.

3.      Limit the display to only the last 10 lines for better readability.


Step 1: Creating the FactBox

We define a ListPart Page that retrieves and displays the last 10 posted invoice lines.

page 50100 "MPA Posted SI Lines Factbox"

{

    PageType = ListPart;

    SourceTable = "Sales Invoice Line";

    Caption = 'Posted Sales Invoice Lines';

    RefreshOnActivate = true;

    SourceTableTemporary = true;

    SourceTableView = SORTING("Posting Date", "Line No.") ORDER(Descending);

    ApplicationArea = All;

    DeleteAllowed = false;

 

    layout

    {

        area(content)

        {

            repeater(Group)

            {

                field("Document No."; Rec."Document No.") { ApplicationArea = All; }

                field("Line No."; Rec."Line No.") { ApplicationArea = All; }

                field(Description; Rec.Description) { ApplicationArea = All; }

                field(Quantity; Rec.Quantity) { ApplicationArea = All; }

                field("Unit Price"; Rec."Unit Price") { ApplicationArea = All; }

                field(Amount; Rec.Amount) { ApplicationArea = All; }

            }

        }

    }

 

    trigger OnFindRecord(Which: Text): Boolean

    begin

        exit(LoadDataFromOnFindRecord);

    end;

 

    procedure LoadDataFromOnFindRecord(): Boolean

    var

        SalesInvoiceLines: Record "Sales Invoice Line";

        NoFilter: Code[20];

        Counter: Integer;

        CurrentFilterGroup: Integer;

    begin

        Counter := 0;

 

        CurrentFilterGroup := Rec.FilterGroup();

        Rec.FilterGroup(4);

        NoFilter := Rec.GetFilter("No.");

        Rec.FilterGroup(CurrentFilterGroup);

        Rec.Reset();

        Rec.DeleteAll();

        SalesInvoiceLines.Reset();

        SalesInvoiceLines.SetRange("No.", NoFilter);

        SalesInvoiceLines.SetCurrentKey("Posting Date", "Line No.");

        SalesInvoiceLines.Ascending(false);

 

        if SalesInvoiceLines.FindSet() then

            repeat

                Rec := SalesInvoiceLines;

                Rec.Insert();

                Counter += 1;

            until (Counter >= 10) or (SalesInvoiceLines.Next() = 0);

 

        exit(not Rec.IsEmpty());

    end;

}

How This Works

✔️ Uses a temporary table to ensure efficient data handling.
✔️ Sorts invoice lines in descending order to get the most recent lines first.
✔️ Limits results to 10 lines using a counter.


Step 2: Extending the Sales Order Page

Now, we integrate the FactBox into the Sales Order Page and link it to the Sales Order Lines.

pageextension 50101 “MPA Sales Order Ext” extends "Sales Order"

{

    layout

    {

        addfirst(factboxes)

        {

            part(PostedSalesInvoiceLinesFactbox; "MPA Posted SI Lines Factbox")

            {

                ApplicationArea = All;

                SubPageLink = "No." = field("No.");

            Provider = SalesLines;

            }

        }

    }

}

How This Works

✔️ Links FactBox to Sales Order Lines using the No. field.
✔️ Ensures the FactBox updates dynamically when the user selects a different Sales Order Line.


Final Outcome

  • The FactBox will now display the latest 10 posted sales invoice lines linked to the selected Sales Order Line.
  • Every time a user navigates through different Sales Order Lines, the FactBox updates accordingly.



The code is available at: GitHub Repository.

No comments:

Post a Comment