Rollback inside try function

MLCaptions
Member Posts: 2
Hey fellas
Using the 2016 edition, I'm looking for a way to rollback transactions inside a try function if an error occurs - as would otherwise happen if an error occured in a piece of code.
Specifically I'm calling an XMLPort from a CodeUnit in a try function.
From the CodeUnit i call the XMLPort on an XML file.
Inside the XMLPort I import the fields into a temporary table and then transfer the values into a new Sales Header using validates.
If a validate is unsuccesful (throws an error) I would like for the changes to be rolled back.
Error or not, the calling CodeUnit should know what happened and rename the inputted XML file depending on whether it threw an error or not.
Hope you catch my drift and got any possible solutions.
Thanks in advance.
Using the 2016 edition, I'm looking for a way to rollback transactions inside a try function if an error occurs - as would otherwise happen if an error occured in a piece of code.
Specifically I'm calling an XMLPort from a CodeUnit in a try function.
From the CodeUnit i call the XMLPort on an XML file.
Inside the XMLPort I import the fields into a temporary table and then transfer the values into a new Sales Header using validates.
If a validate is unsuccesful (throws an error) I would like for the changes to be rolled back.
Error or not, the calling CodeUnit should know what happened and rename the inputted XML file depending on whether it threw an error or not.
Hope you catch my drift and got any possible solutions.
Thanks in advance.
0
Best Answer
-
Hi,
I would recommend restructuring the code, so you don't have to write it in a Try Function.
Instead I would make use of CODEUNIT.RUN to handle the errors, so it would look a bit more like this..
YourCodeunit.SetFileToImport(File);
IF YourCodeunit.RUN THEN
HandleSuccess(File)
ELSE
HandleError(File)
This way you can be sure the transaction is rolled back, if an error occurs.1
Answers
-
Hi,
I would recommend restructuring the code, so you don't have to write it in a Try Function.
Instead I would make use of CODEUNIT.RUN to handle the errors, so it would look a bit more like this..
YourCodeunit.SetFileToImport(File);
IF YourCodeunit.RUN THEN
HandleSuccess(File)
ELSE
HandleError(File)
This way you can be sure the transaction is rolled back, if an error occurs.1 -
Try ASSERTERROR ERROR('') - this will give you a silent rolback and will not stop executing the code.
It does not have to be inside Try function.
But generally I agree with nick_robo - ideally you should not need that function. The best would be to restructure the code so the system is left intact when an error occurs.
Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-031 -
Very nice, the Codeunit solution was exactly what I was looking for.
Thanks for the replies, guys.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
- 320 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