Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / Cannot insert duplicat...
Sales forum

Cannot insert duplicate Key

Posted on by 15

Hello,

I am using CRM Sdk with asp.net mvc and very frequently i am getting 

An unhandled exception occured while processing the request.

Fault exception: Cannot insert duplicate key.in Microsoft.xrm.Sdk.Client.OrganisationServiceContext(Savechangesoptions)

But after restart azure server it is working.

0576.MicrosoftTeams_2D00_image.png

Thanks in Advance

  • Praveen Malik Profile Picture
    Praveen Malik 15 on at
    RE: Cannot insert duplicate Key

    Thanks for your Reply

  • Suggested answer
    Inogic Profile Picture
    Inogic 24,094 on at
    RE: Cannot insert duplicate Key

    When using OrganizationContext make sure you clear the context after every save operation before u request SaveChanges again.

    Everytime a SaveChanges is called the changes are committed to CRM and the updates received from the platform are copied to the objects in the context. To give you an example u added a new object to the context and called save - this would create the record the in CRM and also update the object with the Guid. 

    If the context is not cleared and you have modifications to submit the next time - this one flows through with an add operation once again and throws a Duplicate Key error since the Guid already exists and u are requesting an ADD operation for this.

    Clear the context and that should fix the problem.

    Sam

  • Praveen Malik Profile Picture
    Praveen Malik 15 on at
    RE: Cannot insert duplicate Key

    I checked Duplication rules also there are no such rule which i am duplicating.

    Once this error came then it will come in all entites insert or update.

    Thanks

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: Cannot insert duplicate Key

    Hi Praveen,

    Please check if data is maintained in the cache memory, as code starts working once you restart the Azure service.

    Also, check Dynamic CRM in which fields duplicate detection is enabled and then check the code if those fields are assigned with a valid value and not the duplicate value.

    Please clear the value of a variable which is used to set value in the field.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans