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

OnBeforeDelete do a check, prevent record from being deleted.

(0) ShareShare
ReportReport
Posted on by 380

Hello,

I know this is probably a basic thing I am over looking, but cannot get it to work. Basically, in our setup, users are able to delete orders with items that are shipped but not invoiced. We want to be able to prevent this from happening. I placed code in the OnBeforeDelete trigger of the Service Line table, but it still is letting the record be deleted. My code is below. 

    trigger OnBeforeDelete();
    begin 
        if ("Quantity Shipped" > "Quantity Invoiced")
        then Error('Cannot Delete Line %1 because there are lines Shipped not Invoiced',"Line No.");
    end;

I also want to know if I put this code in the line level, and they click delete on the header level of the order, will it stop and prevent all lines from being deleted.

Thanks,

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

    There is no OnBeforeDelete, if you are planning to subscribe to that event then it should like below

    LOCAL [EventSubscriber] OnBeforeDelete(VAR Rec : Record "Service Line";RunTrigger : Boolean)

    ERROR('Test');

  • ChazKimRP Profile Picture
    380 on at

    I may not be understanding you but there is a trigger for OnBeforeDelete? So I do not know what I am doing wrong based on your response.

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

    Where did you add the above code? in the table?

  • ChazKimRP Profile Picture
    380 on at

    Yes in the Service Line Table I added that. Trying to prevent service orders from getting deleted when they have quantities shipped not invoiced.

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

    Can you please send the screenshot of it, there is a OnDeleteTrigger but not OnBeforeDelete, OnBeforeDelete is the event which you have to subscribe.

  • ChazKimRP Profile Picture
    380 on at

    Hi Suresh,

    Per your request see screen shot of available triggers for a Table Extension. (I removed my previous code)

    pastedimage1578402000515v1.png

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

    Those are subscriber events, you need to subscribe to them. Create a codeunit a subscribe for ex: for the table sales line for OnBeforeModifyEvent, do similar for service line OnBeforeDelete

      [EventSubscriber(ObjectType::Table, 37, 'OnBeforeModifyEvent', '', false, false)]

       local procedure OnBeforeModify(var Rec: Record "Sales Line"; RunTrigger: Boolean)

       var

           SalesHeader: Record "Sales Header";

       begin

           if not RunTrigger then

               exit;

           SalesHeader.Get(Rec."Document Type", Rec."Document No.");

       end;

  • Community Member Profile Picture
    on at

    There are two different concepts here: the trigger and the event.

    What you are using is the trigger. This is only called when then delete trigger is called, i.e. either the record is deleted manually or when a Delete(true) is called from code. My guess is than when the header is deleted, it calls a Delete(false) on the lines; thus, your OnBeforeDelete trigger is not run.

    What you need is an event subscriber:

    [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnBeforeDeleteEvent', '', false, false)]

       local procedure OnBeforeDeleteEvent(var Rec: Record "Sales Line");

       begin

           if (Rec."Quantity Shipped" > Rec."Quantity Invoiced") then

               Error('Cannot Delete Line %1 because there are lines Shipped not Invoiced', Rec."Line No.");

       end;

  • Bodhi Profile Picture
    558 on at

    Like PConijn said:

    What you are using is the trigger. This is only called when then delete trigger is called, i.e. either the record is deleted manually or when a Delete(true) is called from code. My guess is than when the header is deleted, it calls a Delete(false) on the lines; thus, your OnBeforeDelete trigger is not run.

    We can try to use Delete(true)  in the code when the header is being deleted

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 2,664

#2
YUN ZHU Profile Picture

YUN ZHU 960 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 773 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans