Catching Errors In Navision -- Useful even for the Experts

girish.joshigirish.joshi Member Posts: 407
edited 2007-01-08 in NAV Tips & Tricks
We all know (and if you don't search the forum) that the only way to catch errors in Navision is the
If codeunitA.RUN then

structure. We also know that you can programatically call codeunits with the this structure
Codeunit.run( Codeunit's Object Number )

However, its less known that when using the latter structure anywhere in the cal stack, it will cause errors from the previous structure to NOT be caught.

To put it another way, if you use the if codeunit.run() structure anywhere in the call stack, you can't catch any errors. Very important when writing code that use the Job Scheduler.

Comments

  • bstanneveldbstanneveld Member Posts: 8
    We all know (and if you don't search the forum) that the only way to catch errors in Navision is the

    Code:
    If codeunitA.RUN then


    structure.

    It is not true that this is the only way to catch errors with codeunits. There is really no difference between calling the
    RUN()
    
    method of a codeunit variable, or just writing
    CODEUNIT.RUN(Number)
    
    If you call the latter example the same way using the IF structure, you can catch the errors in the very same way.

    There is only one advantage to using a codeunit variable: you can initialize the codeunit by calling user defined functions that set global variables, and you can obtain the result of the transaction by calling user defined functions that return status, or whatever that is stored in global vatriables.

    The difference between calling the RUN procedure (any way) with the IF structure, is that Navision handles the erros by aborting - and rolling back - the transaction when you don't use the IF structure. If this is not wat you want, or you need custom error handling, you can use the IF structure. It just works like the GET from the RECORD variable type.
  • garakgarak Member Posts: 3,263
    edited 2007-01-08
    We've also used an sheduler working on NAS.

    But i needed the information about the error.
    I needed the whole error message.
    With other words, the message that is display if there where an userinterface. But nas doesnt have Guid.

    So i've changed my code to folloing:

    loop through Seduling setup records
    if CU.Run(Number in ShedulingRecord) ...
      ....
      DeleteExistingErrorlogs (Table in Navision, include the ShedulingRecID, Date, an Flag if send)
    end else
      exist an Errorlog in the ErrorLogTable for this CUJob on this day?
        No:
          Create an errorlog in the ErrorLogTable
          CU.Run() <- to get the whole Errormessage <- NAS write it into System.Eventlog
        yes
          is the message not send?
            Get the Errorlog from System.EventLog
            Send the Message in System.Eventlog per Mal to me
            set the Errorlog in ErrorLogTable as send
          end           
    end;
    

    With this, i can see, why the job is not running.
    Do you make it right, it works too!
  • bstanneveldbstanneveld Member Posts: 8
    Hello,

    I havn't tried that yet, but I sure will sometime in the future. I've been writing custom error handling code that sets an error message to be retrieved by a custom function. It sure is a lot of work. You end up with more error catching code than anything else! Your solution will save me a lot of time!
Sign In or Register to comment.