web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

error while deleting sales order

(0) ShareShare
ReportReport
Posted on by

Dear Experts,

I need to delete blank sales orders. Please advise. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    What error are you getting ?

  • Community Member Profile Picture
    on at

    error-sale-order.png

  • Suggested answer
    Amol Salvi Profile Picture
    18,698 on at

    I think somebody has created sales invoice for this order . Kindly check.

  • Suggested answer
    Binesh Profile Picture
    7,885 on at

    Agree with [tag:Amol],

    use debugger and check where you get exception. you will get idea.

  • Suggested answer
    Alexander Ermakov Profile Picture
    28,096 on at

    There is also Delete Invoiced Sales Orders Batch Job exists (msdn.microsoft.com/.../hh179047(v=nav.70).aspx)

  • Suggested answer
    Tharanga Chandrasekara Profile Picture
    23,118 on at

    Agreed with others. It seems invoice has been created for the order. You can go to sales invoice page and check for the created invoice.

  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    Clear the posting No. from the header and then delete the order. If there is posting No. assigned on the header it will try to delete the invoice which is what causing the issue.

  • Community Member Profile Picture
    on at

    Hi All,

    Thanks for your support. I have tried to remove the order no and delete but it is not getting deleted as it is linked with no. series and PSS ,PSI(Posted Sales Shipment, Posted Sales Invoice). I have check the code as well all documents are interlinked. These sales orders are blank and are very old from the year 2013 to 2016 (6-7 sales orders) which I need to delete. Please advise.

    IF ("Document Type" = "Document Type"::Quote) AND ("Doc. Sub Type" = "Doc. Sub Type"::Quotation) THEN BEGIN

     IF ("Sent for Approval") AND (NOT Approved) THEN

       ERROR(Text5003,"Document Type","No.");

    END;

    IF DOPaymentTransLogEntry.FINDFIRST THEN

     DOPaymentTransLogMgt.ValidateCanDeleteDocument("Payment Method Code","Document Type",FORMAT("Document Type"),"No.");

    IF NOT UserSetupMgt.CheckRespCenter(0,"Res Center") THEN

     ERROR(

       Text022,

       RespCenter.TABLECAPTION,UserSetupMgt.GetSalesFilter);

    IF ("Opportunity No." <> '') AND

      ("Document Type" IN ["Document Type"::Quote,"Document Type"::Order])

    THEN BEGIN

     IF Opp.GET("Opportunity No.") THEN BEGIN

       IF "Document Type" = "Document Type"::Order THEN BEGIN

         IF NOT CONFIRM(Text040,TRUE) THEN

           ERROR(Text044);

         TempOpportunityEntry.INIT;

         TempOpportunityEntry.VALIDATE("Opportunity No.",Opp."No.");

         TempOpportunityEntry."Sales Cycle Code" := Opp."Sales Cycle Code";

         TempOpportunityEntry."Contact No." := Opp."Contact No.";

         TempOpportunityEntry."Contact Company No." := Opp."Contact Company No.";

         TempOpportunityEntry."Salesperson Code" := Opp."Salesperson Code";

         TempOpportunityEntry."Campaign No." := Opp."Campaign No.";

         TempOpportunityEntry."Action Taken" := TempOpportunityEntry."Action Taken"::Lost;

         TempOpportunityEntry.INSERT;

         TempOpportunityEntry.SETRANGE("Action Taken",TempOpportunityEntry."Action Taken"::Lost);

         PAGE.RUNMODAL(PAGE::"Close Opportunity",TempOpportunityEntry);

         IF Opp.GET("Opportunity No.") THEN

           IF Opp.Status <> Opp.Status::Lost THEN

             ERROR(Text043);

       END;

       Opp."Sales Document Type" := Opp."Sales Document Type"::" ";

       Opp."Sales Document No." := '';

       Opp.MODIFY;

       "Opportunity No." := '';

     END;

    END;

    SalesPost.DeleteHeader(

     Rec,SalesShptHeader,SalesInvHeader,SalesCrMemoHeader,ReturnRcptHeader,SalesInvHeaderPrepmt,SalesCrMemoHeaderPrepmt);

    VALIDATE("Applies-to ID",'');

    ApprovalMgt.DeleteApprovalEntry(DATABASE::"Sales Header","Document Type","No.");

    SalesLine.RESET;

    SalesLine.LOCKTABLE;

    WhseRequest.SETRANGE("Source Type",DATABASE::"Sales Line");

    WhseRequest.SETRANGE("Source Subtype","Document Type");

    WhseRequest.SETRANGE("Source No.","No.");

    WhseRequest.DELETEALL(TRUE);

    SalesLine.SETRANGE("Document Type","Document Type");

    SalesLine.SETRANGE("Document No.","No.");

    SalesLine.SETRANGE(Type,SalesLine.Type::"Charge (Item)");

    DeleteSalesLines;

    SalesLine.SETRANGE(Type);

    DeleteSalesLines;

    // 016 +++++++++++++

    IF ("Document Type" IN ["Document Type"::Quote,"Document Type"::Order]) THEN BEGIN

     //TESTFIELD("Lost Reason");

     // UPDATE STATUS ON LEAD

     IF (("Document Type" = "Document Type"::Quote) AND ("Doc. Sub Type" = "Doc. Sub Type"::Quotation))

       OR ("Document Type" = "Document Type"::Order) THEN BEGIN

       LeadHeader.RESET;

       LeadHeader.SETRANGE(LeadHeader."Document Type",LeadHeader."Document Type"::Quote);

       LeadHeader.SETRANGE(LeadHeader."No.","Lead No.");

       IF LeadHeader.FINDFIRST THEN BEGIN

          LeadHeader."Lost Reason" := "Lost Reason";

          LeadHeader.VALIDATE(LeadHeader."Followup Status",LeadHeader."Followup Status"::Lost);

          LeadHeader.MODIFY;

       END;

     END;

    END;

    // 016 *************

    SalesCommentLine.SETRANGE("Document Type","Document Type");

    SalesCommentLine.SETRANGE("No.","No.");

    SalesCommentLine.DELETEALL;

    GateEntryAttachment2.RESET;

    GateEntryAttachment2.SETRANGE("Source Type",GateEntryAttachment2."Source Type"::"Sales Return Order");

    GateEntryAttachment2.SETRANGE("Entry Type",GateEntryAttachment2."Entry Type"::Inward);

    GateEntryAttachment2.SETRANGE("Source No.","No.");

    IF GateEntryAttachment2.FINDFIRST THEN

     GateEntryAttachment2.DELETEALL;

    StrOrder.SETCURRENTKEY("Document Type","Document No.",Type);

    StrOrder.SETRANGE("Document Type","Document Type");

    StrOrder.SETRANGE("Document No.","No.");

    StrOrder.SETRANGE(Type,StrOrder.Type::Sale);

    IF StrOrder.FINDFIRST THEN

     StrOrder.DELETEALL;

    StrOrderLine.SETCURRENTKEY("Document Type","Document No.",Type);

    StrOrderLine.SETRANGE("Document Type","Document Type");

    StrOrderLine.SETRANGE("Document No.","No.");

    StrOrderLine.SETRANGE(Type,StrOrder.Type::Sale);

    IF StrOrderLine.FINDFIRST THEN

     StrOrderLine.DELETEALL;

    IF (SalesShptHeader."No." <> '') OR

      (SalesInvHeader."No." <> '') OR

      (SalesCrMemoHeader."No." <> '') OR

      (ReturnRcptHeader."No." <> '') OR

      (SalesInvHeaderPrepmt."No." <> '') OR

      (SalesCrMemoHeaderPrepmt."No." <> '')

    THEN BEGIN

     COMMIT;

     IF SalesShptHeader."No." <> '' THEN

       IF CONFIRM(

            Text000,TRUE,

            SalesShptHeader."No.")

       THEN BEGIN

         SalesShptHeader.SETRECFILTER;

         SalesShptHeader.PrintRecords(TRUE);

       END;

     IF SalesInvHeader."No." <> '' THEN

       IF CONFIRM(

            Text001,TRUE,

            SalesInvHeader."No.")

       THEN BEGIN

         SalesInvHeader.SETRECFILTER;

         SalesInvHeader.PrintRecords(TRUE);

       END;

     IF SalesCrMemoHeader."No." <> '' THEN

       IF CONFIRM(

            Text002,TRUE,

            SalesCrMemoHeader."No.")

       THEN BEGIN

         SalesCrMemoHeader.SETRECFILTER;

         SalesCrMemoHeader.PrintRecords(TRUE);

       END;

     IF ReturnRcptHeader."No." <> '' THEN

       IF CONFIRM(

            Text023,TRUE,

            ReturnRcptHeader."No.")

       THEN BEGIN

         ReturnRcptHeader.SETRECFILTER;

         ReturnRcptHeader.PrintRecords(TRUE);

       END;

     IF SalesInvHeaderPrepmt."No." <> '' THEN

       IF CONFIRM(

            Text055,TRUE,

            SalesInvHeader."No.")

       THEN BEGIN

         SalesInvHeaderPrepmt.SETRECFILTER;

         SalesInvHeaderPrepmt.PrintRecords(TRUE);

       END;

     IF SalesCrMemoHeaderPrepmt."No." <> '' THEN

       IF CONFIRM(

            Text054,TRUE,

            SalesCrMemoHeaderPrepmt."No.")

       THEN BEGIN

         SalesCrMemoHeaderPrepmt.SETRECFILTER;

         SalesCrMemoHeaderPrepmt.PrintRecords(TRUE);

       END;

    END;

  • Suggested answer
    Saurabh Shah Profile Picture
    4,560 on at

    Hello Mandeep,

    Can you please check below:

    Open sales order, Go to Invoices action and check whether any number (As given in error) is displayed.

    If there is no invoices there, Please go to Posted sales invoices and check by filtering the order number with the same order.

    Let us know.

  • AbduTeeyech Profile Picture
    428 on at

    Did you try Alexander 's suggestion? to use delete invoiced sales orders option in nav?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans