Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Deleting older sales orders

(0) ShareShare
ReportReport
Posted on by 60

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.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,802 Super User 2024 Season 2 on at
    RE: Deleting older sales orders

    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

  • Suggested answer
    TeddyH Profile Picture
    TeddyH 12,864 Super User 2024 Season 1 on at
    RE: Deleting older sales orders

    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.

  • MarisolC Profile Picture
    MarisolC 60 on at
    RE: Deleting older sales orders

    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.

  • MarisolC Profile Picture
    MarisolC 60 on at
    RE: Deleting older sales orders

    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!

  • Rebe.Gwaindepy Profile Picture
    Rebe.Gwaindepy 162 on at
    RE: Deleting older sales orders

    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

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,802 Super User 2024 Season 2 on at
    RE: Deleting older sales orders

    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

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! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans