Dynamics CRM 2016 Online – Cannot insert duplicate key error (Possible causes and solution)
Very recently I was helping a business acquaintance of mine with their existing CRM Online implementation and I ran into a rather peculiar problem. I will describe the scenario first, then the error we faced and finally possible solution/workaround.
If your case does not fall in the scenario described here, there can be n number of reasons you are getting this error and you should start digging deep in traces logs or engage your CRM Administrator and/or Microsoft Support.
The client had multiple environments (Dev, Testing, Staging and Prod) which were cloned from 1 root environment and over the period of time they went out of sync (It is quite common to be these environments out of sync in terms of data as well as customization)
– however it was not tracked when they went out of sync. Now comes the strange part. There were certain records which needed to be migrated between two environments. I suggested we use standard import/export feature of Dynamics CRM. It should have worked but it did not.
We tried a lot but the files never went through and it never showed us any error. We gave up and as there were just 70 odd records, I whipped up a small utility that will take these records from source and push them to target. All set right? Well… NO.
Error and Troubleshooting:
I started getting error: Cannot insert duplicate key. I was surprised as the possible duplicate records were indeed not present in other environment. Dedupe rules were all disabled. Alternate keys were not defined. I disabled Activity Feed rules as well. Still no results.
As a last resort, I removed all the unnecessary attributes (which included some relationships) and taadaa… it all went in… except 1 record. After careful evaluation of both the environments I found that another record with the same GUID was already present (As I cloned records from other environment GUIDs were same).
So there were multiple reasons I was getting these errors in this scenario
- I had relationship attributes cloned, which retained GUIDs
- 1 record already had a duplicate record in the target environment. – As the environments were cloned their GUID seeds were same in each organization.
A possible solution here is to remove relationship data while you are cloning records and re-establish them after the records are created and also do a cleanup of record ID so you don’t run in to the second case.