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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

copy records through plugin

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I am writing a plugin to copy the related records of an entity. I was able to copy the parent record and stuck in copying related records. I've retrieved related records through query expression and I am trying to create new records(duplicates).

Here is the code snippet that is throwing error: "Cannot insert duplicate key".
                            if (listCaseParticipants != null)//listCaseParticipants has the retrieved entitycollection
                            {
                                foreach (Entity caseparticipants in listCaseParticipants)
                                {
                                    Entity caseparticipantentity = caseparticipants;
                                    caseparticipantentity.Attributes.Remove(caseparticipantentity.LogicalName + "id");//this line throws error.
                                    Guid relatedCasePId = service.Create(caseparticipantentity);

                                 }

                               }

How should I create new copy of each retrieved record?

Please guide me through this. Thanks for any help.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    saemmang Profile Picture
    205 on at

    Hi,

    You could use an InitializeFromRequest for that:

    https://msdn.microsoft.com/en-us/library/jj863602.aspx

    This will copy the records values.

    Steve

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Roxanna,

    The error you're getting isn't removing the key attribute (that code is fine, I tested the same code with Contact). The problem is in the Create call. Try explicitly setting a new key on your entity like this:

    caseparticipantentity[caseparticipantentity.LogicalName + "id"] = Guid.NewGuid();

    Guid relatedCasePId = service.Create(caseparticipantentity);

    This will prevent you from getting the duplicate key exception.

    Hope this helps! I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

     Aiden

  • crmprogrammer2013 Profile Picture
    2,665 on at

    Thanks Aiden,

    But I am getting this error: 'Entity Id must be the same as the value set in property bag'

  • saemmang Profile Picture
    205 on at

    Hi Roxanna,

    The property caseparticipantentity.Id must be the same as the attribute caseparticipantentity[caseparticipantentity.LogicalName + "id"].

    So you could add the following line to your code to change the property:

    caseparticipantentity.Id = Guid.Empty;


    Please don't assign self-created GUIDs to new records. Let the system create the new Id for you. Otherwise it may impact the performance of CRM.

    That's why it would be a cleaner solution to use the InitializeFromRequest.

    Steve

  • crmprogrammer2013 Profile Picture
    2,665 on at

    Hi Steve,

    You are right. I've used 'InitializeFromRequest' instead and was able to complete this.

    It's indeed a much cleaner way of doing like you said.

    Thank you so much.

    Merry Christmas. :):):)

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans