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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

(0) ShareShare
ReportReport
Posted on by 147

Hi

I am creating a clone of record on it's create. Plugin is registered on post-create of the record.

 QueryExpression qe = new QueryExpression("contact")
            {
                ColumnSet =  new ColumnSet("firstname", "lastname")
            };
            EntityCollection entityCollection = _organizationService.RetrieveMultiple(qe);
            foreach (Entity entity in entityCollection.Entities)
            {                
                if (!entity.Attributes.Contains("firstname"))
                {
                    entity.Attributes.Add("firstname", "");
                }
                entity["firstname"] = (entity.GetAttributeValue<string>("firstname") ?? "") + "(Copy)";

                _organizationService.Create(entity);
            }

If I do not Entity.Id = Guid newGuid() for the clone record, it gives me "Cannot insert duplicate key"

If I add Entity.Id = Guid newGuid(), it gives me "Entity Id must be the same as the value set in the property bag.

Can you help please?

Thanks
Arj

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    1. If you are creating a clone copy of just created record from plugin, you dont need another RetrieveMultiple. Simply take the values from target entity.

    2. The below line will create a new record and assign a new GUID as per sequence without duplicate values. So dont assign a new Guid() for that record.

    _organizationService.Create(entity);

  • Suggested answer
    Community Member Profile Picture
    on at

    just do entity.Attributes.Remove("contactid")

    but as it stands you will be causing an infinite chain.  When does it NOT .Create(entity)?

  • Suggested answer
    Pawar Pravin Profile Picture
    5,237 on at

    You could create clone record without retrieving data in plugin. Please refer below url for reference:

    www.briteglobal.com/.../

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    Hi,

    Just want to give you information. In Microsoft Code Review. There is one section that inform to never use the same object to be put in Service.Create / Service.Update.

    The correct way is:

    var entityCollection = _organizationService.RetrieveMultiple(qe);
    foreach(var entity in entityCollection.Entities) 
    {
     var firstName = entity.GetAttributeValue("firstname") ?? "";
     var newEntity = new Entity("contact");
     newEntity["firstname"] = firstName    "(Copy)";
      _organizationService.Create(newEntity);
    }

  • Suggested answer
    Martin Donnelly Profile Picture
    1,030 on at

    this new Entity("contact") is not a solution.  the OP wants to clone NOT just make a new record with a similar firstname; this code misses all the other attributes.  this will work if you iterate through entity.Attributes and newEntity.Attributes.Add() everything EXCEPT Attribute["contactid"] -- that's the attribute causing the duplicate key error.

  • Martin Donnelly Profile Picture
    1,030 on at

    the second error about the difference between the entity.Id and the property bag is because the contactid exists in two places -- the "header" (Entity.id) AND the property bag (Entity.Attributes["contactid"].  to get past this error set the header to GUID.Empty and remove the attribute (CRM will assign a new GUID on its own.)

  • Suggested answer
    Martin Donnelly Profile Picture
    1,030 on at

    and, finally, if you want a "business" clone, copy the original Attributes["createdon"] and add it as Attributes["overriddencreatedon"]

    !

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans