if codeunit.run, Try Functions, asserterror and associates

Hi, an external system is writing in a customized table in dynamics NAV (2018) (called interface table) which I process and then write an item-journal line and then post it.

I am validating the value of the fields in that interface table using try functions and in case of error I mark the record as error so the external system can retrieve it and eventually correct the error shown (for instance: Item code field is empty). This all works fine, but the problem comes when I try to post the line. I have found no way of catching eventual posting errors in order avoid an standard error message for posting (for instance: Not enough stock of product xxxx)



How can I catch the error before it raises? Try function wont work since the procedure (codeunit 22) writes into the database. "IF codeunit.run Item Jnl.-Post Line" is not allowed for similar reasons...

I have checked the preview posting for sales header, but it is kind of tricky since the "Preview" flag is all around codeunit 80 and this is not happening with 22 Item Jnl.-Post Line where I need it.

Any ideas?

Best Answer

Answers

  • KTA8KTA8 Member Posts: 388
    If you have a flag and the end of each process, if you go there it's correct if you have an error the code wouldn't get there.
  • achecac@yahoo.com[email protected] Member Posts: 11
    Thanks KTA8 but I was wondering if there is a better way to handle any type of errors without having to use flags or similar...
  • Dynamics2018Dynamics2018 Member Posts: 24
    edited 2018-05-21
    Hello All,

    I have solved the problem. It was standard product Bug.

    Page 10146 "Deposit List" in North America version has not designed properly in Demo Product. Please see below snapshot for more details.

    I have designed page properly and it has solved the problem.

    ofbt2jva1job.png
  • achecac@yahoo.com[email protected] Member Posts: 11
    Setup a separate NST just to handle the external calls, confiure it to allow writes inside try functions (set DisableWriteInsideTryFunctions to false in CustomSettings.config)

    This will enable you use try function to do posting trial and catch the error. But writes are disabled in Try functions for a good reason, so when you enable it make sure to handle the full rollback in you code manually, to ensure that there is no partially committed data left in the system

    The scenario/coding could look like this :
    CLEARLASTERROR;
    PostOK := TryPostItemJournal(ItemJnl);
    If NOT PostOK THEN BEGIN
      RememberLaseErrorMessage(); //store in some var, ie LastErrorMessage := GETLASTERRORTEXT 
      RollBackTransations() ; //
    END
    EXIT(PostOK) // or EXIT(LastErrorMessage)
    
    the rollback function is not READILY available but you can simulate it like this:
    RollBackTransations() 
    ASSERTERROR ERROR('')
    






    Cool! I will give it a try! :smile:
Sign In or Register to comment.