if codeunit.run, Try Functions, asserterror and associates

achecac@yahoo.com
Member Posts: 11
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?
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?
0
Best Answer
-
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('')
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035
Answers
-
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.0
-
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...0
-
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('')
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-035 -
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.
0 -
Slawek_Guzek wrote: »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!0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 322 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions