Hello Experts,
is there a way to copy an Entity in C# without knowing the fields?
I will make a Workflow Step where i can copy or clone every Entity in CRM.
Kind Regards,
Léon
*This post is locked for comments
Hi All,
I wanted to use the EntityExtensions' Clone method in order to just clone an existing record using plugin.
Now, I'm using CoreAssemblies 8.2.0.2 and I know that Microsoft.Xrm.Client needs to be separately added. And these two assemblies together are not getting along well.
Any idea of any other replacements of the Microsoft.Xrm.Client so that I can use EntityExtensions' Clone methods instead of cloning the records manually by looping through each field?
Kindly advise.
Thanks.
Hello Andrew,
thank you :) im so blind...
Now it works fine.
Thank you all for your help!
Kind Regards,
Léon
Hello,
1. When you create record using service.Create method id of record is not set automatically but it is returned as a result of call. Replace
service.Create(CloneSeminar);
with line
CloneSeminar.Id = service.Create(CloneSeminar);
2. Looks like you're working with collection in your loop but your code modifies parent record instead of child:
CloneSeminar.Attributes.Remove("mrd_seminarinhaltid");
CloneSeminar.Attributes.Remove("mrd_ak_seminar");
CloneSeminar.Attributes["mrd_ak_seminar"] = CloneSeminar.Id;
Should it be CloneSeminarinhalt instead?
3. I assume that mrd_ak_seminar is lookup field and you populate it with Guid value. During the creation it will throw an error so you should replace line
CloneSeminarinhalt.Attributes["mrd_ak_seminar"] = CloneSeminar.Id;
with line
CloneSeminarinhalt.Attributes["mrd_ak_seminar"] = CloneSeminar.ToEntityReference();
Hi,
You can retrieve entity metadata using retrieveentityrequest and use this metadata to create entity
Following links may be helpful to you:
msdn.microsoft.com/.../microsoft.xrm.sdk.messages.retrieveentityrequest.aspx
msdn.microsoft.com/.../gg509071.aspx
Hope this helps:)
Hello,
so now i can copy entitys but that leads to antoher problem.
If i copy my Entity i want to copy the related Entitys to.
But i can't assigne the related Entity to my "Master" Entity.
The code cant find the ID from my "Master" Entity
This is my code:
//Record clone Entity CloneSeminar = Seminar; CloneSeminar.Id = Guid.Empty; CloneSeminar.Attributes.Remove("mrd_seminarid"); CloneSeminar.Attributes.Remove("mrd_ak_seminarbeginn"); CloneSeminar.Attributes.Remove("mrd_ak_seminarende"); CloneSeminar.Attributes.Remove("mrd_ak_seminarnummer"); service.Create(CloneSeminar); tracingService.Trace("Seminar wurde erstellt"); CloneSeminar.ToEntityReference(); Clone.Set(executionContext, CloneSeminar); // Retrive related Entities QueryExpression query = new QueryExpression("mrd_seminarinhalt"); query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, rref.Id); // Query EntityCollection retrieveedxpan = service.RetrieveMultiple(query); if (retrieveedxpan.Entities.Count == 0) { // Datensätze wurden nicht gefunden tracingService.Trace("Es wurden keine Seminarinhalte gefunden!"); } else { foreach (var entity in retrieveedxpan.Entities) { entity.Attributes.Remove("mrd_seminarinhaltid"); //CloneSeminarinhalt ermitteln Entity CloneSeminarinhalt = entity; CloneSeminarinhalt.Id = Guid.Empty; CloneSeminar.Attributes.Remove("mrd_seminarinhaltid"); CloneSeminar.Attributes.Remove("mrd_ak_seminar"); CloneSeminar.Attributes["mrd_ak_seminar"] = CloneSeminar.Id; //service.Create(CloneSeminarinhalt); tracingService.Trace(Convert.ToString(CloneSeminar.Attributes["mrd_seminarid"])); tracingService.Trace(Convert.ToString(CloneSeminarinhalt.Id)); tracingService.Trace("Seminarinhalt wurde erstellt und zugewießen"); } } }
Hi Leon,
Maybe this will help you out.
community.dynamics.com/.../cloning-a-record-in-dynamics-crm
It has a step by step procedure on creating a clone of a record.
Hope this helps.
I'm really sorry but you will have to write own extension replacing this "Clone" method with your own code.
If you want your code work to work you will have to follow this.
But i work with this .dll :D
This is my code and the "Clone" extentsion only exist in Microsoft.Xrm.Client.
// Seminar ermitteln Entity Seminar = service.Retrieve(rref.LogicalName, rref.Id, new ColumnSet(true)); EntityReference Id = Seminar.ToEntityReference(); //Record klonen Entity newSeminar = EntityExtensions.Clone(Seminar, false); Seminar.Id = Guid.Empty; //service.Create(newRecord); tracingService.Trace(Convert.ToString(newSeminar.LogicalName)); QueryExpression query = new QueryExpression("mrd_seminarinhalt"); query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, Id); // Query ausfürhen EntityCollection retrieveedxpan = service.RetrieveMultiple(query); if (retrieveedxpan.Entities.Count == 0) { // Datensätze wurden nicht gefunden tracingService.Trace("Es wurden keine Seminarinhalte gefunden!"); } else { foreach (var entity in retrieveedxpan.Entities) { //ChildRecord ermitteln Entity ChildSeminar = new Entity("mrd_seminarinhalt"); //ChildRecord klone var newChildSeminar = EntityExtensions.Clone(ChildSeminar, false); newChildSeminar.Id = Guid.Empty; tracingService.Trace(Convert.ToString(newChildSeminar.LogicalName)); //service.Create(newChildRecord); } }
Leon,
Remove reference on Microsoft.Xrm.Client. This assembly is not available in the sandbox.
Hello Karsten,
my code is know ready, but if i try to execute them in my CRM than i got this error and i dont know how to fix it..
<Message>Unexpected exception from plug-in (Execute): WorkflowActivityLibrary5.SeminareKopieren: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Xrm.Client, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.</Message>
Kind Regards,
Léon
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156