Skip to main content

Notifications

Announcements

No record found.

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;
  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: AL procedure to run action Modify Item No for Sales Order

    Its Great

    Thanks for Update.

  • lionx Profile Picture
    lionx 2 on at
    RE: AL procedure to run action Modify Item No for Sales Order

    I resolved this myself. Thank you.

  • lionx Profile Picture
    lionx 2 on at
    RE: AL procedure to run action Modify Item No for Sales Order

    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;

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: AL procedure to run action Modify Item No for Sales Order

    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;

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans