Breaking-out from a trigger

mehrozmehroz Member Posts: 12
Can anyone tell me how to break-out from OnDelete() trigger without the Error(ErrMessageText) function.

e.g
OnDelete()

If myCondition = True
  Break the trigger(i.e. dont delete the record)

Answers

  • ufukufuk Member Posts: 514
    Why don't you want to show a message? If you tell more about your case we can find a more suitable method.
    Ufuk Asci
    Pargesoft
  • Yaroslav_GaponovYaroslav_Gaponov Member Posts: 158
    Hi

    NAV way is all code for business and logical must be in table trigger.

    Rollback without message: ERROR('');
  • mehrozmehroz Member Posts: 12
    edited 2007-10-25
    Actually I am trying to run some code after these OnDelete, OnInsert, OnModify triggers have been completed; i.e. I want some OnAfterDelete, OnAfterInsert and OnAfterModify triggers.
    For this I did something like this:
    OnDelete() Trigger
    
      {  some code here }
    
      Delete; //manually delete the row from the table
    
      { since the row has been deleted so now this block will have the same effect as some OnAfterDelete trigger }
     
    




    The problem with the above logic is that after the code in OnDelete trigger is completed, navision also tries to delete the same row that has already been deleted with my Delete statement above and so I get a 'row not found error'. So i want navision to break-out from this trigger as i have manually handled it. Also, since no error has been raised and the row has been deleted successfully so i dont want to annoy user with some error message. Can u have a solution for this?
  • mehrozmehroz Member Posts: 12
    Hi Yaroslav

    Thanks for your solution. However it didnt solve my problem since the Error(''); statement rollbacks the changes and I just wanted Navision to do nothing since I have handled the event myself.
  • kinekine Member Posts: 12,562
    Something like:
    If MyCondition then
      Exit;
    

    :?: 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DenSterDenSter Member Posts: 8,305
    Why would you want to delete the record when that is handled by the OnDelete trigger itself? As soon as the trigger ends, the record gets removed from the database. Once you go into the trigger, there's no going back. I think when you delete the record as part of the OnDelete trigger, you'll get an error message saying 'record doesn't exist' or something.

    You can prevent the trigger from running altogether by calling DELETE with a FALSE parameter:
    MyRecord.DELETE(FALSE); // deletes and does not run OnDelete
    MyRecord.DELETE(TRUE); // deletes and runs OnDelete
    
    I think what you will want to do is copy the code that you need from the OnDelete trigger and make that part of the code that actually deletes the record.
  • EugeneEugene Member Posts: 309
    Do not put Rec.DELETE statement in Rec's own OnDelete trigger.
    On Delete trigger is issued right before the record is going to be deleted so you dont need to delete that record in the code - it will be deleted right after you exit the trigger.

    What is exactly you trying to do after deleting the record ? Why cant that code be executed before the record is deleted ? You understand that after the record is deleted you will not know what record was deleted (unless you save somewhere its primary key values). IF you cant execute your code in onDelete trigger then probably you will need at end of the trigger set some boolean variable to true and check that variable periodically.

    Are you sure you want to do your code on table level and not on interface level (i.e on a certain form) ?
  • mehrozmehroz Member Posts: 12
    Thanks for all of your replies. I am now placing a new button to run the code that I wanted to run in some sort of OnAfterDelete() trigger.
Sign In or Register to comment.