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 :
Small and medium business | Business Central, N...
Suggested Answer

Deleting older sales orders

(0) ShareShare
ReportReport
Posted on by 103

Hi, I haver this requirement:

I need to delete all sales orders that are more than 15 days old (it doesn´t matter if they were covered totally o partially). I wrote some code and it works perfect, BUT, this sales orders are not archived automatically and I need that they are archived. 

        dFechaInicio := 20220101D;

        dFechaBorrar := CalcDate('<-15D>', WorkDate());
        rcSalesHeader.SetRange("Order Date", dFechaInicio, dFechaBorrar);
        if rcSalesHeader.FindSet() then
            repeat
                rcsalesLine.SetRange("Document No.", rcSalesHeader."No.");
                if rcsalesLine.findset() then begin
                    rcsalesLine.DeleteAll();
                    rcSalesHeader.Delete();
                end;
            until rcSalesHeader.NEXT = 0;
    end;
 
I've thingking that I need to use a different method to achive this because in fact what I want to replicate is as if user was hitting Delete button:
pastedimage1665427978854v1.png
Finding those methods confuses me a lot.
Thanks in advance.
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,329 Super User 2025 Season 2 on at

    Hi, You can run the program below manually.

    Or just run delete trigger in your code.

    For example,  rcSalesHeader.Delete(true);

    pastedimage1665449684552v1.png

    Hope this helps.

    Thanks.
    ZHU

  • Rebe.Gwaindepy Profile Picture
    216 on at

    Hi Marisun 

    In addition to what ZHU said, may you make sure that:

    1. You pass the appropriate filters to your code. From the sample you sent, you are not setting the document type filter therefore the system may delete all sales documents withing the given dates (Return orders,credit memos etc)
    2. Ensure that the Archive Orders field is ticked on sales and receivables setup because the auto archive procedure uses that condition as shown below:

      pastedimage1665473160540v1.png

    Regards,

    Kudah

  • MarisolC Profile Picture
    103 on at

    Zhu, as always you are saving my day, I did run delete trigger and everything ran perfectly. Now, my doubt is, how can I knos which methods, as rcSalesHEader.Delete(true) exists and how can I know which one I need to use?

    TIA!

  • MarisolC Profile Picture
    103 on at

    Besides that, I don't know what makes this different:

    rcSalesHeader.Delete();  

    first when I ran this it only deleted Sales Headers but Sales Lines where still existing which caused me another issues

    from  this:

    rcSalesHeader.Delete(true); Here a complete delete Sales happened: Header, Lines and Archive.

    I cannot fully undestand how this works.

    So sorry to bohter you, I have been learning slowly and some concepts are new to me.

  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,284 Super User 2025 Season 2 on at

    Delete(true) runs the validation inside the delete logic which perform checks and delete all related tables (such as lines, dimension, etc).

    Delete() or Delete(false) basically just delete without validation. Or you can call it force delete which is why the lines remained.

    In general, always run the validation.

  • Suggested answer
    YUN ZHU Profile Picture
    95,329 Super User 2025 Season 2 on at

    Hi, just adding some info.

    This is mentioned in MS Learn | Docs.

    https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-delete-method

    pastedimage1666220168170v1.png

    AL code in the OnDelete Trigger for Sales Header.

    pastedimage1666220239495v2.png

    So if you want to run the code in the OnDelete Trigger of the table, you should use Record.Delete(true).

    In addition, the same is true for Insert, Modify, ModifyAll, DeleteAll, and OnValidate, but the executed code are in different trigger.

    For example,

    pastedimage1666220354472v3.png

    Hope this helps.
    Thansk.

    ZHU

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 > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans