Error handling in Table triggers

ta5ta5 Member Posts: 1,164
Hello experts
I have a strange bevavior in a testing scenario.

There is a table called table1, the modify trigger looks like this
Error('test error');


There is a codeunit with this code
table1.find('-');
table1.code := table1.code + 'x';
if not table1.modify(true) then
  message('my error handling');


If the codeunit is run, guess what message appears. It's not 'my error handling', it's 'test error'.

Whats the reason for this? I thought if the return value of the modify trigger is handled by the code, the error in the respective trigger does not fire ](*,)

Thanks in advance
Thomas

Comments

  • AlbertvhAlbertvh Member Posts: 516
    Hi Thomas,

    when you use table1.MODIFY(TRUE) it fires the OnModify trigger of the table1.
    If you need to get your error message use table1.MODIFY THEN



    Albert
  • ta5ta5 Member Posts: 1,164
    Hi Albert
    Thanks, I know that I could do that. But I need to use the table trigger, because in my real life scenario there are some very complicated checks on this trigger which I cannot duplicate.
    Any other ideas?
  • AlbertvhAlbertvh Member Posts: 516
    Hi Thomas

    Maybe you could remove the error messages in the OnModify trigger
    but then when the table is updated from a form then you would probably
    need these error messages.
    Maybe for your scenario you could use something like the SetHideValidationDialogue of the sales header table.


    Albert
  • ta5ta5 Member Posts: 1,164
    Thanks Albert

    I will check what I can do.

    But I'm still quite wondering why the error is firing when I use the return value of the trigger function. :?
  • AlbertvhAlbertvh Member Posts: 516
    Hi Thomas

    Even though you are using the return value it still runs all the
    code in the OnModify trigger and if you have an ERROR statement this will be executed.



    Albert
  • ta5ta5 Member Posts: 1,164
    Yes, but for example if used with codeunits this is a very handy feature, for example in
    if mycodeunit.run
    

    imho in mycodeunit there can be ANY error and they never fire if the return value is used. So I wonder why this does not work here. Additionally I think I have seen constructs like
    if not insert then modify
    //quite not sure whether (true) was used or not
    
  • MbadMbad Member Posts: 344
    You cant do the check like that unfortunately. What you could do as a simple solution is creating a global variable you can set on the on modify trigger and a function that returns it.

    The "if codeunit.run" option is only available for codeunits.
  • AlbertvhAlbertvh Member Posts: 516
    Hi Thomas

    Ok get your point. What you will have to do is create another codeunit to call your modifying codeunit.

    i.e MyCU1
    IF NOT MyCU2.RUN THEN
      ERROR('Cannot modify my table1');
    

    MyCU2
    table1.find('-'); 
    table1.code := table1.code + 'x'; 
    table1.modify(true) then 
      error('my error handling'); 
    



    Albert
  • ta5ta5 Member Posts: 1,164
    @mbad

    I think you're right. But then what is the return value for table triggers used for?
  • ta5ta5 Member Posts: 1,164
    Hi Everybody
    Someone other has some thoughts on this? Thanks in advance

    Thomas
Sign In or Register to comment.