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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Cannot Insert Duplicate key

(0) ShareShare
ReportReport
Posted on by 287

Hi,

I am trying to associate Account with entity B with N:N relationship in code shown below.


AssociateEntitiesRequest req = new AssociateEntitiesRequest();
req.Moniker1 = new EntityReference { Id = moniker1.Id, LogicalName = moniker1.LogicalName };
req.Moniker2 = new EntityReference { Id = moniker2.Id, LogicalName = moniker2.Name };
req.RelationshipName = strEntityRelationshipName;
service.Execute(req);
tracingService.Trace(string.Format("After AssociateManyToManyEntityRecords"));
return true;

But it is giving me message:-

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.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">
<ErrorCode>-2147220937</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>Cannot insert duplicate key.</Message>
<Timestamp>2018-02-17T09:51:07.251855Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>

[Asociate Women Council: Asociate_Women_Council.Class1]
[f56fea6f-3212-e811-9431-00155d12a139: Asociate_Women_Council.Class1: Update of account]

</TraceText>
</OrganizationServiceFault>

Thanks

Prashant Verma

*This post is locked for comments

I have the same question (0)
  • Jharana Baliyar Singh Profile Picture
    2,667 on at
    RE: Cannot Insert Duplicate key

    Hi Prashant,

    I am also trying to do associate & disassociate records in subgrid but getting error cannot insert duplicate key while updating one lookup value.

    What could be the issue ? & how did ur issue resolved

    Thanks,

    Jharana

  • Prashnat Verma Profile Picture
    287 on at
    RE: Cannot Insert Duplicate key

    Yes the error is obvious & is resolved.

    Thank you so much for the inputs.

  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: Cannot Insert Duplicate key

    Hi Prashant, if it's complaining about the duplicate, there is a duplicate. I am wondering if, maybe, one of those records is inactive, and you are only displaying active records in the subgrid. Or, possibly, you are calling the same plugin twice somehow.

    Deprecation of that method does not mean it should not be working, especially since you are on 2013.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Cannot Insert Duplicate key

    Hi Prashant

    Use IOrganizationService.Associate method to associate the records. As Eric suggested  AssociatedEntitiesRequest has been deprecated, and you should be using the AssociateRequest instead.

    Please go through msdn helps

    msdn.microsoft.com/.../microsoft.xrm.sdk.iorganizationservice.associate.aspx

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: Cannot Insert Duplicate key

    Can you share your latest code, and possibly a screenshot of the entity record you are trying to set the relationship to? The error is pretty obvious that you are trying to insert a duplicate record.

  • Prashnat Verma Profile Picture
    287 on at
    RE: Cannot Insert Duplicate key

    Hi Aric,

    Thanks for the answer but it still shows the same message that cannot insert the duplicate key, and showed no data in the grid.

    Thanks

    Prashant Verma

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: Cannot Insert Duplicate key

    Hi Prashant,

    It seems like that association already exists in your environment.

    You should be put your service.Execute inside a try...catch block, so that it captures errors when trying to associate records.

    The AssociatedEntitiesRequest has been deprecated, and you should be using the AssociateRequest instead.

    Also, you should probably use the following code to associate between entities:

    AssociateRequest req = new AssociateRequest();

    req.Target = new EntityReference("account", new Guid("28B942AE-0B70-E011-8DD8-1CC1DEE8EA59"));

    //RelatedEntities are the entities you are associating to your target (can be more than 1)

    req.RelatedEntities = new EntityReferenceCollection();

    rreq.RelatedEntities.Add(new EntityReference("contact", new Guid("5AC04904-D57B-E011-941A-1CC1DEE8EA59")));

    req.Relationship = new Relationship("contact_customer_accounts");

    //execute the request

    try

    {

      service.Execute(areq);

    }

    catch (System.Exception ex)

    {

    }

    Hope this helps.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#3
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans