Skip to main content

Notifications

Announcements

No record found.

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

OnBeforeDelete do a check, prevent record from being deleted.

Posted on by 372

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,

  • Bodhi Profile Picture
    Bodhi 554 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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;

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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;

  • ChazKimRP Profile Picture
    ChazKimRP 372 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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
    Suresh Kulla 43,745 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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
    ChazKimRP 372 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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
    Suresh Kulla 43,745 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

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

  • ChazKimRP Profile Picture
    ChazKimRP 372 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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
    Suresh Kulla 43,745 on at
    RE: OnBeforeDelete do a check, prevent record from being deleted.

    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');

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans