Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Please help! Can't create new Entities anymore: Cannot insert duplicate key

Posted on by 1,545

We update our Production environment using a Managed Solution once per week.

Yesterday we had an error on a specific entity when trying to import: Cannot insert duplicate key. Even if this entity does not exist in Prod.

So we removed the entity from the solution, failed again. Now another Entity was target as being a duplicate Key.

For the sake of it, we tried to create a new entity directly in Production, in an unmanaged Solution. Same error: Cannot insert duplicate key.

So the problem seems to come from our Prod environment. We can't create new entities anymore. Whatever the name is, we get the error. The error log is below but it doesn't say much.

We use CRM 8.2.2.112 on-premise.

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Cannot insert duplicate key.Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ActivityId>a71860cb-9822-4c54-81f7-e2cc96d1d0fd</ActivityId>
<ErrorCode>-2147220937</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>Cannot insert duplicate key.</Message>
<Timestamp>2018-09-26T10:03:03.2318156Z</Timestamp>
<ExceptionRetriable>false</ExceptionRetriable>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</OrganizationServiceFault>

*This post is locked for comments

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Great !!!. Glad it is resolved :)

  • Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    That’s a good news Yvan. Thanks for keeping us updated!

  • Verified answer
    yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    We found the problem.

    The duplicate I deleted at first was not the only problem.

    As you may know, every custom entity in CRM gets a new Entity Type Code appearing from 10000 onwards.

    Our most recently created entity was numbered 10155.

    After many hours, we found one extra CustomControlDefaultConfig that was related to EntityTypeCode 10156, which (you probably see me coming here) did not exist in CRM.

    So every time we were trying to create a new entity, CRM was trying to increment the new entity and its related CustomControlDefaultConfig with EntityTypeCode = 10156.

    But there was already one! (well, 2 at the very beginning). How can that be? I don't know. If we had ever created said entity, then deleted it, the next entity to be created should have been 10157 (we tried to force it, but could not find where). But there never was a 10156 entity. So the CustomControlDefaultConfig related to it was an oprhan record in the Database.

    I guess this bug appreared during an import or an upgrade.

    Once we deleted that CustomControlDefaultConfig related to EntityTypeCode 10156, everything went back to normal. We were able to create new entities, and Import the most recent solution from our source environment.

    I want to thank Ravi and Rawish. You both helped me get things figured out. I also wanna thank my buddy JFG, who went under the hood of CRM without knowing CRM at all.

  • yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    @Ravi

    I checked! I have 552 entities, all are unique. No duplicates.

    Thanks again. No luck still.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Hi,

    Can you check if you have any duplicte entity type code-

    select * from EntityView order by ObjectTypeCode

  • yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Hi Ravi, yes I Published. The trace looks like this:

    Level: Error |ReqId: 6db2c74d-3d03-45a8-9c26-69de336833a5 |ActivityId: 6db2c74d-3d03-45a8-9c26-69de336833a5 | CustomControlDefaultConfigService.ValidateDuplicatePrimaryEntityTypeCode ilOffset = 0xFA
    at CustomControlDefaultConfigService.ValidateDuplicatePrimaryEntityTypeCode(IBusinessEntity entity, ExecutionContext context) ilOffset = 0xFA
    at CustomControlDefaultConfigService.Create(IBusinessEntity entity, ExecutionContext context) ilOffset = 0x19
    at EntityService.CreateUIData(EntityCreateInfo entityInfo, ExecutionContext context) ilOffset = 0x71
    at EntityService.CreateInternal(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context) ilOffset = 0x4E
    at EntityService.Create(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context) ilOffset = 0xB0
    at EntityService.Create(EntityCreateInfo entityInfo, ExecutionContext context) ilOffset = 0x1A
    at EntityService.Create(EntityCreateInfo entityInfo, Guid solutionId) ilOffset = 0x2E
    at EntityCreate.Execute(Guid solutionId, ParameterBag paramBag) ilOffset = 0x1C1
    at SystemCustomization.CreateEntity(XmlNode data) ilOffset = 0x11
    at RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) ilOffset = 0xFFFFFFFF
    at RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) ilOffset = 0x16
    at RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) ilOffset = 0x7F
    at LogicalMethodInfo.Invoke(Object target, Object[] values) ilOffset = 0x3F
    at WebServiceHandler.Invoke() ilOffset = 0xAE
    at WebServiceHandler.CoreProcessRequest() ilOffset = 0x134
    at SyncSessionlessHandler.ProcessRequest(HttpContext context) ilOffset = 0x3F
    at CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() ilOffset = 0x18D
    at HttpApplication.ExecuteStepImpl(IExecutionStep step) ilOffset = 0x64
    at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) ilOffset = 0x15
    at PipelineStepManager.ResumeSteps(Exception error) ilOffset = 0x27A
    at HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) ilOffset = 0x31
    at HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) ilOffset = 0xB0
    at PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) ilOffset = 0x131
    at PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) ilOffset = 0x0
    at UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus& notificationStatus) ilOffset = 0xFFFFFFFF
    at UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus& notificationStatus) ilOffset = 0xFFFFFFFF
    at PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) ilOffset = 0x1E7
    at PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) ilOffset = 0x0
    >Crm Exception: Message: Cannot insert duplicate key., ErrorCode: -2147220937
    [2018-09-28 08:27:56.824] Process: w3wp |Organization:e649666f-8b8c-e611-80e1-005056b929fd |Thread: 114 |Category: Platform.Metadata |User: 00000000-0000-0000-0000-000000000000 |Level: Error |ReqId: 6db2c74d-3d03-45a8-9c26-69de336833a5 |ActivityId: 6db2c74d-3d03-45a8-9c26-69de336833a5 | EntityService.Create ilOffset = 0xB0
    >EntityService.Create caught exception: Microsoft.Crm.BusinessEntities.CrmDuplicateRecordException: Cannot insert duplicate key.
    à Microsoft.Crm.ObjectModel.CustomControlDefaultConfigService.ValidateDuplicatePrimaryEntityTypeCode(IBusinessEntity entity, ExecutionContext context)
    à Microsoft.Crm.ObjectModel.CustomControlDefaultConfigService.Create(IBusinessEntity entity, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.CreateUIData(EntityCreateInfo entityInfo, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.CreateInternal(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.Create(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context)
    [2018-09-28 08:27:56.839] Process: w3wp |Organization:e649666f-8b8c-e611-80e1-005056b929fd |Thread: 114 |Category: Application |User: 00000000-0000-0000-0000-000000000000 |Level: Error |ReqId: 6db2c74d-3d03-45a8-9c26-69de336833a5 |ActivityId: 6db2c74d-3d03-45a8-9c26-69de336833a5 | AppWebService.CreateSoapException ilOffset = 0x4F
    >MSCRM Error Report:
    --------------------------------------------------------------------------------------------------------
    Error: Cannot insert duplicate key.

    Error Number: 0x80040237

    Error Message: Cannot insert duplicate key.

    Error Details: Cannot insert duplicate key.

    Source File: Not available

    Line Number: Not available

    Request URL: http://URL.../AppWebServices/SystemCustomization.asmx

    Stack Trace Info: [CrmDuplicateRecordException: Cannot insert duplicate key.]
    à Microsoft.Crm.ObjectModel.CustomControlDefaultConfigService.ValidateDuplicatePrimaryEntityTypeCode(IBusinessEntity entity, ExecutionContext context)
    à Microsoft.Crm.ObjectModel.CustomControlDefaultConfigService.Create(IBusinessEntity entity, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.CreateUIData(EntityCreateInfo entityInfo, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.CreateInternal(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.Create(EntityCreateInfo entityInfo, MetadataHelper metadataHelper, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.Create(EntityCreateInfo entityInfo, ExecutionContext context)
    à Microsoft.Crm.Metadata.EntityService.Create(EntityCreateInfo entityInfo, Guid solutionId)
    à Microsoft.Crm.Application.WebServices.SystemCustomization.EntityCreate.Execute(Guid solutionId, ParameterBag paramBag)
    à Microsoft.Crm.Application.WebServices.SystemCustomization.SystemCustomization.CreateEntity(XmlNode data)

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Hi Yvan,

    After deleting did you try publish all customization before creating a new entity? Can you reproduce the issue and capture teh trace again? It could be some other issue as well

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Hi Yvan, yes you are right i was thinking the same.

    Since that is a managed environment we cant event really do much. Else i was thinking if we can export a solution from target itself and comment out all customcomtrolconfig tags and import it back cause we cant delete them using sql query.

    But what i feel is its a high time that you should connect with microsoft and raise a high priority ticket. But yeah feel free to try above suggestion if possible after taking the backup in out of office hours.

  • yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Thanks again for your time Rawish. I tried your new steps and it doesn't work either. There are no more customcontroldefaultconifig tags in the customization.xml file, but the import still fails.

    In the image below, you can see where the import always fails. It's when the import tries to create a new entity in the target system. So my understanding is that the import can update existing entities, but is not able to create the new entity.

    2018_2D00_09_2D00_28_5F00_06_2D00_40_2D00_17.jpg

    This is where I started thinking the problem is in the target system. When I realized this, I though ok, I will try to manually create a new entity manually in the target system. But even when I do that, I get this message below. I tried this many times in the past few days and it always fails. The fact that I can't do that tells me the target is the problem.

    2018_2D00_09_2D00_28_5F00_06_2D00_40_2D00_17.jpg

    This means the target system won't accept new entities . In the source environment, I don't have this problem. I can create as many entities as I want.

    I imported updates of this solution many times in the past, with no problem. Last update was in August and worked fine. The new update contains a new entity, and now it fails.

  • Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Please help! Can't create new Entities anymore: Cannot insert duplicate key

    Hi Yvan,

    Indeed its very irritating and i understand your situation.

    Lets try one more thing - might be a bit time consuming but this might work.

    1. Export the managed  solution from your source system.

    2. Extract the customization XML

    3. for each entity look for customcontroldefaultconifig tags and comment them out.  

    4. Save the file, Zip the solution files back and import to target.

    Note : Please keep a neat solution with your before making changes just to be on a safer side.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans