try catch UpdateConflict template

try catch UpdateConflict / DuplicateKeyException template:
final class VKTest
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        #OCCRetryCount

        int infologLine;

        try
        {
            infologLine = infolog.num();
            info(strFmt("Execution: %1, infolog line: %2", xSession::currentRetryCount(), infologLine));

            ttsbegin;
            // simulating update conflict error
            CustTable   custTable,
                        custTable2;

            select forupdate firstonly custTable;

            select forupdate firstonly custTable2;
            
            custTable.Memo = '111';

            custTable.update();

            custTable2.Memo = '222';

            custTable2.update();

            ttscommit;
        }
        #StandardRetryCatchBlock
    }

}
Execution:

Content of #OCCRetryCount marco:
#define.RetryNum    (5)     //number of retries in catch(Exception::UpdateConflict) loop
    
#localmacro.StandardRetryCatchBlock
catch (Exception::UpdateConflict)
{
    if (appl.ttsLevel() == 0)
    {
        if (xSession::currentRetryCount() >= #RetryNum)
        {
            throw Exception::UpdateConflictNotRecovered;
        }
        else
        {
            retry;
        }
    }
    else
    {
        throw Exception::UpdateConflict;
    }
}
catch(Exception::DuplicateKeyException)
{
    if (appl.ttsLevel() == 0)
    {
        if (xSession::currentRetryCount() >= #RetryNum)
        {
            throw Exception::DuplicateKeyExceptionNotRecovered;
        }
        else
        {
            retry;
        }
    }
    else
    {
        throw Exception::DuplicateKeyException;
    }
}
#endmacro




Support The Author

 If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.

Buy Me a Coffee

Post a Comment


All Comments


No comments. Be the first one to comment on this post.

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own "buy me a coffee" link.
Join us.

Blog Tags