Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

Sales Order Deletion after Manually creating Sales Invoice

(2) ShareShare
ReportReport
Posted on by 111

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;
 

 

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Share Your Event with the Community

Jump start your event engagement! 📢

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,646 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans