Rollback inside try function

MLCaptionsMLCaptions 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.

Best Answer

  • nick_robbonick_robbo Member Posts: 49
    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.

Answers

  • nick_robbonick_robbo Member Posts: 49
    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.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    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-03
  • MLCaptionsMLCaptions Member Posts: 2
    Very nice, the Codeunit solution was exactly what I was looking for.

    Thanks for the replies, guys.
Sign In or Register to comment.