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

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

How can we know if a record is being deleted?

(0) ShareShare
ReportReport
Posted on by 40

Sometimes it is necessary to develop a certain logic that involves parent and child elements.

For example, we want to compose the manufacturing route in the header (RoutingHeader) based on the work centers or machines in the lines (RoutingLines).

 

Therefore, every time you add a new operation, change it or delete it, you must update this field.

 

If we delete the header directly, it will trigger the deletion of the lines that causes the maintenance of the header to give an error because the element is marked for deletion. Is there any property in the header that tells us that it is being deleted so that this logic is not fired? We would need to be able to distinguish between the deletion of the header and the deletion of the lines.

Trying to find analogous elements in the system we could think that orders with their lines would have the same problem but this problem does not occur since the fields of the header that are related to the lines are FlowFields.

 

In our scenario we want to concatenate data so the FlowFlied field is not useful.

 

Any idea? Do you consider necessary to know if the element is being deleted?

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: How can we know if a record is being deleted?

    Hello,

    If you do not get further traction or responses in this forum, it may be because we currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,166 Most Valuable Professional on at
    RE: How can we know if a record is being deleted?

    In the documents you mentioned, deletion of lines is handled in the OnDelete trigger of the Header table.

    If this trigger is executed, the lines are deleted, otherwise not. In code, when you call a Delete you can decide if the OnDelete trigger must be executed or not.

    Delete(true) executes the OnDelete trigger, Delete(false) does not execute the OnDelete trigger (so only the header is removed).

  • DavidES Profile Picture
    40 on at
    RE: How can we know if a record is being deleted?

    Thank you Stefano and also Marco!

    With this proposal we need to redefine the action of delete and the lines can be orphaned. In our scenario we want to delete also the lines. I will try to put a better example. In the Route Header we have a new field "Route Composition". This field is calculate by code when a route line is created, modified or deleted. For example: if I have two operations Cutting and Bending the system calculates that field with Cutting - Bending.

    We are listening to the OnAfterDelete event of the lines to recalculate this value. When the header is deleted the lines will be deleted as propagation since this is implemented in the BC standard. When recalculating it we are modifying a record that is then deleted. Hence the need to know if the main element has been deleted.

    I'm going to try creating some boolean in the lines and have the header mark it but I don't think it's a very good solution.

    Thank you.

  • ArturV Profile Picture
    225 on at
    RE: How can we know if a record is being deleted?

    Hi David. I don't fully understand what is the expected result you want to achieve when the Routing Header is about to be removed.

    I understand how does it work during the validation

    GIVEN Routing Header with one routing line of the operation "Cutting"

    WHEN Add an additional routing line of the operation "Bending"

    THEN The "Route Composition" is "Cutting-Bending"

    Why it's important to handle the value of the "Rout Composition" field during the deletion of the header record while you anyway delete it? Please explain and i will try to help you.

  • DavidES Profile Picture
    40 on at
    RE: How can we know if a record is being deleted?

    Thank you Artur for answer me!

    Calculating the route composition is not the problem. The main problem is that when a routing line is deleted I need to recalculate again the route composition. So in the case I have "Cutting" and "Bending" the route composition is "Cutting-Bending" as you explain. Now I delete the line of "Bending". The route composition is recalculated to only "Cutting".

    In case that someone deletes the whole route from the header my logic tries to recalculate the route composition because the lines are deleted automatically by BC and I receive the following message:

    pastedimage1590645461452v1.png

    The message makes sense because I have modify a record that will be deleted (I suppose that the system reads it) but I don't know any option to know if the line deletion is made from the header (in this scenario I don't want to recalculate it because it will be deleted) or from the lines (I need to recalculate it). So imagine that in the logic to calculate the route composition when I recover the RoutingHeader I can check if the RoutingHeader.MarkedForDeletion I can exit my logic.

    I have prepare a basic code that maybe helps to understand it better.[View:/cfs-file/__key/communityserver-discussions-components-files/758/DeleteRoute.zip]

    This scenario can also happen in other parent child elements such as orders with their lines but the standard uses FlowField fields that do not cause this problem.

    Thank you!

  • Verified answer
    ArturV Profile Picture
    225 on at
    RE: How can we know if a record is being deleted?

    Hi David,

    Thanks for the detailed explanation, now it's clear. You're right, there is no chance to understand if the parent record is about to be deleted so you need to mark it. Here is my way of doing this:

    tableextension 50100 "Routing Header CUS" extends "Routing Header"

    {

       fields

       {

           field(50100; "Routing Abbreviation"; Text[100])

           {

               DataClassification = CustomerContent;

               Caption = 'Routing Abbreviation';

           }

           field(50101; "Skip Rtng. Abbreviation Calc."; Boolean)

           {

               DataClassification = CustomerContent;

               Caption = 'Skip Rounting Abbreviation Calculation';

           }

       }

       trigger OnBeforeDelete()

       begin

           "Skip Rtng. Abbreviation Calc." := true;

           Modify();

       end;

    }

    Then the condition of the update function in the lines should be changed:

       local procedure UpdateRoutingHeaderAbbreviation()

       var

           RoutingHeader: Record "Routing Header";

           RoutingLines: Record "Routing Line";

           RoutingQuickView: TextBuilder;

       begin

           if RoutingHeader.Get("Routing No.") and not RoutingHeader."Skip Rtng. Abbreviation Calc." then begin

    ...

  • DavidES Profile Picture
    40 on at
    RE: How can we know if a record is being deleted?

    Thank you Artur!

    I tried your proposal and managed to delete the header, thank you!

    It would be nice if this was in the standard to avoid going all this way.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sumit Singh Profile Picture

Sumit Singh 2,085

#2
YUN ZHU Profile Picture

YUN ZHU 1,810 Super User 2025 Season 2

#3
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 1,788

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans