Hello everyone,
As per our business requirement, we generate Sales Invoices from Sales Shipments, ensuring that the invoice is linked to both the Sales Shipment and the Sales Order.
Once the Sales Invoice is posted and the entire Sales Order is fully invoiced, the corresponding Sales Order still remains in the Sales Order list. To handle this, we implemented a functionality that archives the Sales Order before deleting it.
I have a concern regarding a specific line of code:
SalesOrderHeader.Delete();
I’d like to understand the impact of this deletion on customers and items. Does deleting the Sales Order here behave the same way as the standard process when invoicing and posting directly from the Sales Order card (where the Sales Order is automatically removed, leaving only the posted Sales Invoice)?
Below is the code snippet for reference. If there are any mistakes or better approaches, Please suggest:
[EventSubscriber(ObjectType::Codeunit, Codeunit::ArchiveManagement, 'OnBeforeAutoArchiveSalesDocument', '', false, false)]
local procedure ArchiveManagement_OnBeforeAutoArchiveSalesDocument(var SalesHeader: Record "Sales Header"; var IsHandled: Boolean)
var
ArchiveManagement: Codeunit ArchiveManagement;
SalesInvoiceLine: Record "Sales Line";
PostedSalesShipmentHeader: Record "Sales Shipment Header";
SalesOrderHeader: Record "Sales Header";
SalesLine: Record "Sales Line";
ShipmentNo: Text;
SalesOrderNo: Code[20];
SalesPost: Codeunit "Sales-Post";
EverythingInvoiced: Boolean;
begin
if SalesHeader."Document Type" = SalesHeader."Document Type"::Invoice then begin
// Iterate through Sales Invoice Lines to extract the Shipment No.
SalesInvoiceLine.SetRange("Document Type", SalesInvoiceLine."Document Type"::Invoice);
SalesInvoiceLine.SetRange("Document No.", SalesHeader."No.");
if SalesInvoiceLine.FindSet() then begin
repeat
if StrPos(SalesInvoiceLine.Description, 'Shipment No.') > 0 then begin
ShipmentNo := SalesInvoiceLine.Description;
ShipmentNo := DelStr(ShipmentNo, 1, StrPos(ShipmentNo, 'Shipment No.') + StrLen('Shipment No.'));
ShipmentNo := DelChr(ShipmentNo, '<>', ':');
ShipmentNo := DelChr(ShipmentNo, '<'); // Remove unwanted characters
// Fetch the related Posted Sales Shipment Header
if not PostedSalesShipmentHeader.Get(ShipmentNo) then begin
exit; // If shipment is not found, exit
end;
SalesOrderNo := PostedSalesShipmentHeader."Order No."; // Get related Sales Order No.
SalesOrderHeader.SetRange("Document Type", SalesOrderHeader."Document Type"::Order);
SalesOrderHeader.SetRange("No.", SalesOrderNo);
if SalesOrderHeader.FindFirst() then begin
// Check if everything is invoiced for the Sales Order
SalesLine.SetRange("Document Type", SalesLine."Document Type"::Order);
SalesLine.SetRange("Document No.", SalesOrderNo);
EverythingInvoiced := true; // Assume everything is invoiced
if SalesLine.FindSet() then begin
repeat
if SalesLine."Quantity Invoiced" <> SalesLine.Quantity then begin
EverythingInvoiced := false;
break;
end;
until SalesLine.Next() = 0;
end;
if EverythingInvoiced then begin
// Archive the Sales Order if fully invoiced
ArchiveManagement.ArchiveSalesDocument(SalesOrderHeader);
// Delete the Sales Order after successful invoicing and archiving
SalesOrderHeader.Delete();
end;
end;
exit; // Exit after processing the first valid shipment
end;
until SalesInvoiceLine.Next() = 0;
end;
end;
end;