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;