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.
Its Great
Thanks for Update.
I resolved this myself. Thank you.
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;
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;
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,134 Super User 2024 Season 2
Martin Dráb 229,928 Most Valuable Professional
nmaenpaa 101,156