Skip to main content

Notifications

Announcements

No record found.

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"

Posted on by 145

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

  • Suggested answer
    Martin Donnelly Profile Picture
    Martin Donnelly 1,010 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

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

    !

  • Martin Donnelly Profile Picture
    Martin Donnelly 1,010 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

    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
    Martin Donnelly 1,010 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

    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.

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

    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
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

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

    www.briteglobal.com/.../

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

    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
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: CRM Plugin: "Entity Id must be the same as the value set in the property bag" and "Cannot insert duplicate key"

    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);

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