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, ...
Answered

Try and Catch Duplicate Key

(0) ShareShare
ReportReport
Posted on by

Hi all,

When inserting a record using Try and Catch, can I write like this :

Try
{
    table........ // assign fields
    table.insert();
}
catch (Exception::DuplicateKeyExecption)
{
}

So it is just empty bracket for the "Catch", so its like I don't want the error message of Duplicate key to show up, it's just continue with no insert... and move on.

Bte what is Exception::DuplicateKeyExceptionNoReceovered means ? and the different with the one I used above.

Thanks,

I have the same question (0)
  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    Look at docs.microsoft.com/.../xpp-exceptions for the difference.

    As a best practice I would use

     

    if(table.ValidateWrite())
    {
        table.insert();
    }

  • Community Member Profile Picture
    on at

    Hi Vinit,

    Yes, I read that, but still not understand.  What does it meant (the NotRecovered) cannot caught inside transaction ? means if we're using ttsbegin/ttscommit, it is useless to use that ?

    and more important is actually can I do what I just written ? by just make it empty.

    Thanks,

  • udaY-ch Profile Picture
    5,133 on at

    Hi,

    Check this out,

    catch(Exception::DuplicateKeyException)
        {
            // retry in case of an duplicate key conflict
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
                    throw Exception::DuplicateKeyExceptionNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::DuplicateKeyException;
            }
        }

    I can use rich formatting- sorry

    Uday.

  • udaY-ch Profile Picture
    5,133 on at

    Hi,

    Check this out,

    catch(Exception::DuplicateKeyException)
        {
            // retry in case of an duplicate key conflict
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
                    throw Exception::DuplicateKeyExceptionNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::DuplicateKeyException;
            }
        }

    Uday.

  • Verified answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    If your insert operation is violating primary key of the table the duplicate exception is going to be thrown anyways , you can not avoid that and keep continue your insert operation. Why cant you check if the record with the same key exist or not and avoid error instead of trying to suppress the error message and let exception handling everything for you.

  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi,

    Inside catch(DuplicateKeyException) you can write Retry then system will try to insert the record again with the retry count set which is usually 5(I think). but if it fails for all 5 times then exception is caught by DuplicateKeyExceptionNotRecovered and then you need to take appropriate action on it if any.

    For your scenario correct way would be like below:

    if(!testTable::exists(key))

    {

        testTable.insert();

    }

  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    If you are running it in a loop and want to insert other records  after the current record fails write continue in the catch else write retry.

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 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans