Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

Posted on by Microsoft Employee

Hi all,

I've encountered a very strange error and could not find any solutions for it on the web. Situation is this:

We are using NAV 2009 R2. Company is huge, there are lots and lots of bulk data in the db and we are trying to clean that up a bit. 

There are approx. 4k UNPOSTED Invoice/Cr Memos in the Sales Header and related tables. It also goes for purchase too. These are not used we are certain of that and users can not delete these by themselves cause of permissions.

So, I've asked for an excel file from the accountants which lists No.'s of these invoices to be deleted, and wrote this block:

RecNo := 0;
ExcelBuf.SETFILTER(ExcelBuf."Row No.",'<>1');
TotalRecNo := ExcelBuf.COUNT;
IF ExcelBuf.FINDSET THEN BEGIN
  REPEAT
    RecNo := RecNo + 1;
    Window.UPDATE(1,ROUND(RecNo / TotalRecNo * 10000,1));

    CASE TRUE OF
      (ExcelBuf.xlColID = 'A'):
      BEGIN
            InvoiceNo := ExcelBuf."Cell Value as Text";
            CRLF[1] := 10;
            InvoiceNo := DELCHR(InvoiceNo,'=',CRLF);

            CRLF[1] := 13;
            InvoiceNo := DELCHR(InvoiceNo,'=',CRLF);

            IF InvoiceType = InvoiceType::"Sales Invoice" THEN BEGIN
              recSalesHeader.RESET;
              recSalesHeader.SETRANGE("Document Type", recSalesHeader."Document Type"::Invoice);
              recSalesHeader.SETFILTER("No.", InvoiceNo);
              recSalesHeader.DELETEALL(TRUE);
              {
              SalesHeaderResult := recSalesHeader.GET(recSalesHeader."Document Type"::Invoice, InvoiceNo);
              IF SalesHeaderResult THEN BEGIN
                recSalesHeader.DELETE(TRUE);
              END;
              }
            END
            ELSE IF InvoiceType = InvoiceType::"Purchase Invoice" THEN BEGIN
              IF recPurchaseHeader.GET(recPurchaseHeader."Document Type"::Invoice, InvoiceNo) THEN BEGIN
                recPurchaseHeader.DELETE(TRUE);
              END;
            END
            ELSE IF InvoiceType = InvoiceType::"Sales Credit Memo" THEN BEGIN
              IF recSalesHeader.GET(recSalesHeader."Document Type"::"Credit Memo", InvoiceNo) THEN BEGIN
                recSalesHeader.DELETE(TRUE);
              END;
            END
            ELSE IF InvoiceType = InvoiceType::"Purchase Credit Memo" THEN BEGIN
              IF recPurchaseHeader.GET(recPurchaseHeader."Document Type"::"Credit Memo", InvoiceNo) THEN BEGIN
                recPurchaseHeader.DELETE(TRUE);
              END;
            END;
      END;
    END;
  UNTIL ExcelBuf.NEXT = 0;
END;
Window.CLOSE;

Imported the Excel, process started, some of Invoices from the top of the excel are deleted. Suddenly, it stops the process, no roleback. (36 has commit after delete.) 

The first IF block is altered version. I've tried several versions but first, the error:

SalesHeaderErr.png

Debugger :

SalesHeaderDeb.png

I am checking the SH, specified Invoice No in the error is deleted.

I import the exact same excel again, first 20 rows (which are deleted from the table and all related tables when i first imported the excel) gives no error, so code block works, a new group of rows after the first 20 deleted again, and boom again the same error with different Invoice No.

I've tried to setrange it to bypass GET. Because at first i've wrote first IF block as the purchase part of the code. and get the debugger error at GET.

And also as you can see from the closed block, I've created a boolean variable which only holds IF i get the record or not.

ALL 3 ended with this error.

By the way, I've checked the Sales Header/ line tables' codes for any errors, confirms etc. There is none..

It smells like a bug but i can not be sure.

Are there anyone who encountered the same issue?

Regards,

Can

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    I've tried it just now. Still no luck, same error after a few invoices.

  • Jan Veenendaal Profile Picture
    Jan Veenendaal 510 on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    Had a similar issue a few years back, that I circumvented in this way:

    Create two codeunits. The first codeunit loops through your excelbuffer and selects the document type and no. . Then it calls (a function in) the second codeunit to do the actual DELETE(TRUE);

    Right after each call, in the first codeunit, CLEAR the reference to the seconde codeunit.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    Tried that too. Still same error. Different invoice nos everytime.

    Regards,

    Can

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    Hi

    How you got the InvoiceType , irrespective of Invoicetype you can get the sale Header and delete ,correct (if not same no. series assigned to Sales Document Types).

    i.e

    recSalesHeader.SETRANGE("Document No.", InvoiceNo);

    IF recSalesHeader.Findfirst then

    recSalesHeader.delete;

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    When i imported excel for the first time, the code was just like you've mentioned. It gave the error, so I've changed and tried different approaches.

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    Hi

    Instead of using Variable IF SalesHeaderResult THEN BEGIN, you can directly use

    IF recSalesHeader.GET(recSalesHeader."Document Type"::Invoice, InvoiceNo) THEN

    recsalesheader.delete(true);

    like what you write for Purchase.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    I've checked again. It does not contain any duplicate value..

    Regards,

    Can

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: NAV 2009 ExcelBuffer Unposted Invoice Deletion Error - Sales/Purchase Header Does Not Exist

    Can you whether the excel file contains any duplicate values.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans