Dynamics 365 Business Central: Move line up/down

Dynamics 365 Business Central

Hi, Readers.
A few days ago we discussed how to set and change the default sort order of a page in Business Central.

I received some questions, one of them is very interesting. Is it possible to move the line up and down on the list page? For example, on the sales order subform.

In this post, I would like to share a simple solution on how to move the line up or down in Business Central. This may not be the best solution, but hope this will help.

Goal: Add new feature of moving line up and down in Sales Order Subform (46, ListPart)

Note: Never modify the keys of the table to move lines up and down.

Okay, let’s start.

1. Add a new field to record the position of the sales line.

2. The new field is automatically assigned a value after the sales line is inserted.

3. Display the new field on the page and modify the default sorting. (You can ignore the “Line No.”)

4. Added new logic and actions.

Publish and test:

Test Video:

Source Code: GitHub | MoveLineUpAndDown

tableextension 50100 SalesLineExt extends "Sales Line"
{
    fields
    {
        field(50100; "ZY Line Position"; Integer)
        {
            Caption = 'Line Position';
            DataClassification = CustomerContent;
        }
    }

    trigger OnAfterInsert()
    begin
        "ZY Line Position" := FindLastLinePosition() + 1;
        Rec.Modify();
    end;

    procedure FindLastLinePosition(): Integer
    var
        SalesLine: Record "Sales Line";
    begin
        SalesLine.Reset();
        SalesLine.SetRange("Document Type", "Document Type");
        SalesLine.SetRange("Document No.", "Document No.");
        SalesLine.SetCurrentKey("ZY Line Position");
        SalesLine.Ascending(true);
        if SalesLine.FindLast() then
            exit(SalesLine."ZY Line Position")
        else
            exit(0);
    end;
}
pageextension 50100 SalesOrderSubformExt extends "Sales Order Subform"
{
    layout
    {
        modify("Line No.")
        {
            Visible = true;
        }
        moveafter(Type; "Line No.")

        addbefore(Type)
        {
            field("ZY Line Position"; Rec."ZY Line Position")
            {
                ApplicationArea = All;
                Editable = false;
            }
        }
    }

    actions
    {
        addbefore("F&unctions")
        {
            action("Move Up")
            {
                Caption = 'Move Up';
                ApplicationArea = All;
                Image = MoveUp;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                ToolTip = 'Move current line up.';

                trigger OnAction()
                begin
                    MoveSalesLine(-1);
                end;
            }
            action("Move Down")
            {
                Caption = 'Move Down';
                ApplicationArea = All;
                Image = MoveDown;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                ToolTip = 'Move current line down.';

                trigger OnAction()
                begin
                    MoveSalesLine(1);
                end;
            }
        }
    }

    trigger OnOpenPage()
    begin
        Rec.SetCurrentKey("ZY Line Position");
        Rec.Ascending(true);
    end;

    Local procedure MoveSalesLine(MoveBy: Integer)
    var
        SalesLine: Record "Sales Line";
    begin
        SalesLine.Reset();
        SalesLine.SetRange("Document Type", Rec."Document Type");
        SalesLine.SetRange("Document No.", Rec."Document No.");
        SalesLine.SetRange("ZY Line Position", Rec."ZY Line Position" + MoveBy);
        if SalesLine.FindFirst then begin
            SalesLine."ZY Line Position" -= MoveBy;
            SalesLine.Modify();
            Rec."ZY Line Position" += MoveBy;
            Rec.Modify();
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL