I have a question about the return value of the ondelete trigger.
the docmentation says that you can check the return value of ondelete to catch errors from the trigger, like so:
if rec.delete(true) then
etc...
but when I conducted this test:
if rec.delete(true) then
message('error handled') ;
and in the ondelete trigger of rec
error('error raised')
the message that popped up was Error Raised. The test suggest that the return value doesn't let you know if there was an error. If that's the case, what's the return value for? I am just missing something?
0
Comments
The parameter of the DELETE method is to control whether Navision executes the code in the table's OnDelete trigger.
Try this:
In your table's OnDelete trigger, write this line of code
Then, change your code to this:
When ou run that code in the debugger, you should see it jump into the table's OnDelete trigger and execute the CONFIRM message. You can then click yes, and you will see that you get the "Error was raised" message, because it would not let you delete the record. If you click No, then it will delete the record, and you should see the "everything went well" message.
RIS Plus, LLC
If after the confirm you say you do want the error, you DO NOT see the 'Error was raised' message. Instead, you simply get an error.
That's my point. The whole point of putting the call inside the if statment is to catch the error. But instead, code execution just stops. I ran into this problem while trying to create a batch routine.
Interestingly, if instead of putting the statment in an if statment like
if rec.delete(true) then ...
first nest the code inside a codeunit's run method, like this
OnRun(Rec) ;
Rec.delete(true)
and then elsewhere.
if codeunit.run then
message('no error') ;
else
message('error raised') ;
We DO see the message.
Am I missing something? Is this the preferred idiom for catching errors in OnDelete? In any case, it seems like something people might want to be aware of.
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
The error runs regardless, and will show regardless, and will terminate a transaction regardless. It has nothing to do with whether the DELETE itself fails or not. It's not the DELETE that fails in my example, it's just another error inside the code of the OnDelete trigger.
I put some more code in a form and tested it this time. First just remove the code from the OnDelete trigger, that won't work in this example. For this test I created a new table with a Code field called 'MyCode'. I also created a blank form with one button and put a record variable called MyRec that references the new table. Here's the button's OnPush code, with some explanation: Now when you run this code you should see the 'Error was raised' message.
The return value simply tells you whether the DELETE method succeeded or not. Any error that is raised inside the will be raised regardless. If you want those not to show up you can use it by running a codeunit inside an IF statement, that surpresses the error message.
RIS Plus, LLC
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
In practice it would look like user presses F4 but the record is not deleted. And indeed it looks confusing - any examples of such behavior in navision standard objects ?
When a user presses F4, they will always get a confirmation message. I don't see what is confusing about that.
RIS Plus, LLC