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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Catch duplicate key exception

(0) ShareShare
ReportReport
Posted on by 650

For D365 Update 9, I am attempting to suppress an error message for the Exception::DuplicateKeyException. (If there is a duplicate key I want to continue the thread of code without inserting the record).   My code works in that it will continue to process related code without inserting the record however I am unable to suppress the error log. My code is here:

void createTaxcode(TaxGroup _newTaxGroup,LogisticsPostalAddress _deliveryAddress )

{

TaxGroupHeading newTaxGroupHeading; //Sales Tax Group

TaxGroupName name;

TaxTable newTaxTable; //Sales Tax Code

TaxData newTaxData; //Sales Tax code value

TaxGroupData newTaxGroupData; //Sales Tax code group assignment

TaxOnItem newTaxOnItem; //Item tax group

;

if(_deliveryAddress.CountryRegionId=="USA") //only create tax codes for US addresses.

{

if(strLen(_newTaxGroup)==9)

{

name = strFmt("%1 %2 %3",_deliveryAddress.ZipCode,_deliveryAddress.State,_deliveryAddress.City);

try

{

ttsbegin;

newTaxGroupHeading.initValue();

newTaxGroupHeading.TaxGroup = _newTaxGroup;

newTaxGroupHeading.TaxGroupName = name;

newTaxGroupHeading.insert();

newTaxTable.TaxCode = _newTaxGroup;

newTaxTable.TaxName = Name;

newTaxTable.TaxAccountGroup = 'PDI';

newTaxTable.TaxCurrencyCode = 'USD';

newTaxTable.TaxRoundOff = 1.00;

newTaxTable.TaxRoundOffType = RoundOffType::RoundUp;

newTaxTable.TaxPeriod = _deliveryAddress.State;

newTaxTable.insert();

newTaxData.initValue();

newTaxData.TaxCode = newTaxTable.TaxCode;

newTaxData.TaxValue = 1.00;

newTaxData.insert();

newTaxGroupData.TaxGroup = newTaxGroupHeading.TaxGroup;

newTaxGroupData.TaxCode = newTaxTable.TaxCode;

newTaxGroupData.insert();

//add the new tax code to the ALL item tax group.

newTaxOnItem.TaxItemGroup = 'ALL';

newTaxOnItem.TaxCode = newTaxGroupData.TaxCode;

newTaxOnItem.insert();

ttscommit;

}

catch(Exception::DuplicateKeyExceptionNotRecovered)

{

warning("Not recovered");

}

catch (Exception::DuplicateKeyException)

{

ttsabort;

}

}

else

{

error(strFmt("Unable to create tax code %1",_newTaxGroup)); //error if we didn't get zip + 4

}

}

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mea_ Profile Picture
    60,286 on at

    Hi Don Shields,

    You may play with infolog to cut this error but instead it would be better to check if record exists before inserting.

  • Don Shields Profile Picture
    650 on at

    This issue is that I actually HAVE already checked and not found the record existing, however there is data coming in over an oData interface and apparently that is multi-threading.  So the order of events is that the 1st transaction did not find the related master record so decided to create it.  Before the 1st transaction can create the related record a 2nd transaction also comes in and doesn't find the related record so also wants to create it (via event handler).  During the time before the 2nd transaction can create the missing value, the 1st one creates it so that when the 2nd transaction tries to create the related record that fails since the 1st one already did it.  When the failure occurs I want to trap the error and prevent it from interrupting processing and also not show up in the log.

    Does that make sense?

    Thanks,

    Don

  • Mea_ Profile Picture
    60,286 on at

    There is standard try, catch, retry pattern in AX. Something like

    try {

    if (!record.exist(..))

    {

    record.insert();

    }

    catch(...)

    {

    retry;

    }

    So in case of duplicate key it will retry the whole "try" block and skip record insertion.

    Another option is to catch exception in the app that is calling AX via oData. 

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

News and Announcements

Season of Giving Solutions is Here!

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
TAHER Mehdi Profile Picture

TAHER Mehdi 3

#2
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

#2
Nakul Profile Picture

Nakul 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans