web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to use retry if transaction failed?

(0) ShareShare
ReportReport
Posted on by 1,552

Hi,

if i have two transactions inside try and catch and what i want is if the 2nd transaction failed then a retry should happen. How can i do that?

try
{
    ttsbegin;
    //logic
    ttscommit;


    ttsbegin;
    //logic
    ttscommit;
}
catch
{
}

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi junior AX,

    I believe that the product documentation will be helpful here: 

    docs.microsoft.com/.../xpp-exceptions

  • junior AX Profile Picture
    1,552 on at

    Hi Nikolaos,

    Thank u but the document doesn't say how to use it. Can u help me with this.

    Also it says the retry should be written in catch block

    And if i want the retry to happen only for thr 2nd transaction does that mean i need to put another try and catch?

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi,

    you trigger retry by writing this line of code:

    retry;

    Your try statement should contain only things that must fail together if an exception occurs.

    So most likely you should have two try statements - one for each transaction. It really depends on the details of your requirement. But you need to think what should succeed or fail together and then put all those in same transaction and try statement.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    You cannot use it that way. The retry command will trigger try again. But in your case, you don't want logic1 to run again because it works fine.

    You can combine logics under one tts.

    For logic1, you can add a rerun check if it worked.

    you can write two different try / catch

  • junior AX Profile Picture
    1,552 on at

    Hi Nikolaos and Ergun,

    1. So should i put try catch for the first transaction and then another try catch for the 2nd transaction

    And not a big try and catch and inside another try and catch for the 2nd transaction?

    And what is the different result for using these two options?

    2. Should i just put retry in the catch? And this will do the retry for how many times?

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi junior AX,

    I recommend that you try to study the documentation, as well as this one: docs.microsoft.com/.../exception-handling-with-try-and-catch-keywords

    Then apply your learnings and play around with your code to learn yourself how it works in different constellations. Some hands on experience is usually very efficient when trying to learn new skills.

    The retry will always retry one time, going back to the beginning of the try statement. If the try fails again, it will again retry one time. And so on. Your responsibility is to design your solution in a way that you don't end up with infinite loops.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    We do not know the requirements for development. It is difficult to say anything for sure, but let me try to explain a little bit about what will happen.

    If there is no second tts outside of try, the first catch block will catch the error. In this case, nesting or singular will not be a difference, but I recommend that you separate it if you are not process-related and will not take action for the other logic in case of error.

    If we go back to Retry, it doesn't actually make much sense to write a retry command for any error. Something must have changed the second time the code ran (Deadlock, UpdateConflict..etc.)

  • junior AX Profile Picture
    1,552 on at

    Hi Nikolaos,

    I tried the following two:

    try
    {
        ttsbegin;
        info("one");
        ttscommit;
    
        try
        {
            ttsbegin;
            info("two");
            throw error("error");
            ttscommit;
        }
        catch
        {
            retry;
        }
    
    }
    catch
    {
        retry;
    }

    and this one 

    try
    {
        ttsbegin;
        info("one");
        ttscommit;
    }
    catch
    {
        retry;
    }
    
    try
    {
        ttsbegin;
        info("two");
        throw error("error");
        ttscommit;
    }
    catch
    {
        retry;
    }

    1. both of them gave me the same result. Which is if the 2nd transaction failed a retry is only done to the 2nd transaction. (i'm not interested in retrying the first transaction, just the second one)
    so what's the difference between these two?

    2. I noticed that the retry is giving me an infinite loop, so how can i only say that i want the retry to happen for three times only for example?

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    As I said, there is not much difference in your case (the only difference is if logic1 gets an error, but it doesn't matter if you put a retry there too) but if there was a code like the one below it would make a difference.

    try
    {
        ttsbegin;
        info("one");
        //ttscommit;//goto after logic2
    
        try
        {
            ttsbegin;
            info("two");
            throw error("error");
            ttscommit;
        }
        catch
        {
            retry;
        }
        ttscommit;
    
    }
    catch
    {
        retry;
    }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    1. What do you mean by "difference"? First of all, they contain different code, so that's one difference. You can study functional differences (or lack of them) by debugging your code.

    2. You can use conditional statements if you want to run your retry (or any code) only if some condition applies: docs.microsoft.com/.../if-and-if-else-statements . You can for example increment some variable with each retry, and if the variable is more than the retry count that you need, then don't call retry. In general retry makes sense only if you expect the error to solve by itself before the retry runs (perhaps DeadlockExceptions).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 565 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans