web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

AL procedure to run action Modify Item No for Sales Order

(0) ShareShare
ReportReport
Posted on by 2

Hello, 

I try to create an AL procedure "Modify Item No for Sales Order" that can be used to run action from Power Automate. I want to start with Item table as this is only one procedure that will be used in the whole process, so the Item No will be pass as a parameter in this procedure. The following is the code, but I confront with some issues. 

Should I use DocumentType and DocumentNo as parameters from the previous steps or I can call inside the procedure? Could you please help me to review the code or give me some ideas to get on the right approach? Thanks for your time and response. 

    procedure ModifySalesOrderLineItem1(ExistingItemNo: Code[20]; NewItemNo: Code[20]; DocumentType: Enum "Sales Document Type"; DocumentNo: Code[20])
    var
        SalesOrderHeader: Record "Sales Header";
        SalesLine: Record "Sales Line";
    begin
        // Retrieve the sales order header record for the sales order line
        SalesOrderHeader.RESET;
        SalesOrderHeader.SETRANGE("Document Type", DocumentType);
        SalesOrderHeader.SETRANGE("No.", DocumentNo);

        IF SalesOrderHeader.FINDFIRST THEN BEGIN
            // Check if the sales order is already released
            IF SalesOrderHeader.Status <> SalesOrderHeader.Status::Released THEN BEGIN
                // Set a filter on the Sales Line record to only include lines for this document
                SalesLine.RESET;
                SalesLine.SETRANGE("Document Type", SalesOrderHeader."Document Type");
                SalesLine.SETRANGE("Document No.", SalesOrderHeader."No.");

                // Loop through all matching sales lines
                IF SalesLine.FINDSET THEN BEGIN
                    REPEAT
                        // Update the item number for the sales order line
                        IF SalesLine."No." = ExistingItemNo THEN BEGIN
                            SalesLine."No." := NewItemNo;
                            //SalesLine.VALIDATE("No.")
                            // Modify the sales order line record
                            SalesLine.MODIFY(TRUE);
                        END;
                    UNTIL SalesLine.NEXT = 0;
                END;
            END
            ELSE BEGIN
                ERROR('Cannot modify item number for released sales order lines.');
            END;
        END;
    END;
I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,788 Moderator on at

    Hi,

    I tried to optimize and correct the code, can you try this?

     procedure ModifySalesOrderLineItem1(ExistingItemNo: Code[20]; NewItemNo: Code[20]; DocumentType: Enum "Sales Document Type"; DocumentNo: Code[20])
        var
            SalesOrderHeader: Record "Sales Header";
            SalesLine: Record "Sales Line";
        begin
            // Retrieve the sales order header record for the sales order line
            IF SalesOrderHeader.get(DocumentType, DocumentNo) THEN BEGIN
                SalesOrderHeader.SuspendStatusCheck(true);
                // Check if the sales order is already released
                // Set a filter on the Sales Line record to only include lines for this document
                SalesLine.RESET;
                SalesLine.SETRANGE("Document Type", SalesOrderHeader."Document Type");
                SalesLine.SETRANGE("Document No.", SalesOrderHeader."No.");
                SalesLine.SETRANGE(type, SalesLine.Type::Item);
                SalesLine.SETRANGE("No.", ExistingItemNo);
                // Loop through all matching sales lines
                IF SalesLine.FINDSET THEN
                    REPEAT
                        // Update the item number for the sales order line
                        SalesLine.validate("No.", NewItemNo);
                        SalesLine.MODIFY(TRUE);
                    UNTIL SalesLine.NEXT = 0;
            END
            ELSE
                ERROR('Cannot modify item number for released sales order lines.');
        END;

  • lionx Profile Picture
    2 on at

    Hi Nitin Verma , 
    Thank you for your response. I have updated the procedure as I want to use Item No as a parameter in the procedure. It worked fine for the Sales Order with OPEN status.

    Now I try to add more logic step to modify Item No by REOPENING and RELEASING again the sales order. Could you please help me to have a look at the following procedure? 

    Thank you, a lot, for your time and assistance. 

        procedure ModifyItemNoForSalesOrder3(ExistingItemNo: Code[20]; NewItemNo: Code[20])
        var
            SalesHeader: Record "Sales Header";
            SalesLine: Record "Sales Line";
            SalesLineCopy: Record "Sales Line";
        begin
            // Find all sales order lines that have the existing item number
            SalesLine.SETRANGE(SalesLine."No.", ExistingItemNo);
            IF SalesLine.FINDSET THEN BEGIN
                REPEAT
                    // Get the sales header for the current sales line
                    SalesHeader.GET(SalesLine."Document Type", SalesLine."Document No.");
                    // Check if the sales order is released
                    IF SalesHeader.Status <> SalesHeader.Status::Released THEN BEGIN
                        // Create a copy of the sales order line to modify
                        SalesLineCopy := SalesLine;
                        // Update the item number for the sales order line copy
                        SalesLineCopy."No." := NewItemNo;
                        SalesLineCopy.Validate("No.");
                        // Modify the sales order line record
                        SalesLineCopy.MODIFY(TRUE);
                    END ELSE BEGIN
                        // Handling logic for released sales orders
                        // First, Reopen the sales order
                        IF SalesHeader.Reopen() THEN BEGIN
                            // Create a copy of the sales order line to modify
                            SalesLineCopy := SalesLine;
                            // Update the item number for the sales order line copy
                            SalesLineCopy."No." := NewItemNo;
                            // Modify the sales order line record
                            SalesLineCopy.MODIFY(TRUE);
                            // Release the sales order again
                            IF NOT SalesHeader.Release() THEN BEGIN
                                // Handle the error when releasing the sales order
                                ERROR('Failed to release the sales order: '   SalesHeader.GetLastErrorText());
                            END;
                        END ELSE BEGIN
                            // Handle the error when reopening the sales order
                            ERROR('Failed to reopen the sales order: '   SalesHeader.GetLastErrorText());
                        END;
                    END;
                UNTIL SalesLine.NEXT = 0;
            END;
        END;

  • lionx Profile Picture
    2 on at

    I resolved this myself. Thank you.

  • Suggested answer
    Nitin Verma Profile Picture
    21,788 Moderator on at

    Its Great

    Thanks for Update.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,850 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,084 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 959 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans