Announcements
Hello Everyone,
I have a requirement that I have to clone the record of the custom entity and all it's child records which are related.
I'm able to clone the record but unable to understand how to clone it's child record and link them.
Kindly suggest me the solution. It would be really appreciated if someone provide me the code to achieve the same.
thanks in advance..
You can clone child records using this: https://github.com/demianrasko/Dynamics-365-Workflow-Tools
It has a function for clone record and clone child records. The limitations are that it will clone all child records, including inactive records (the clones of which will be activated).
Please refer below code to create clone records:
Entity Patient = (Entity)context.InputParameters["Target"];
Entity Contact = (Entity)context.PreEntityImages["Image"];
foreach (KeyValuePair<String, Object> attribute in Contact.Attributes)
{
string attributeName = attribute.Key;
object attributeValue = attribute.Value;
switch (attributeName.ToLower())
{
case "contactid":
break;
case "ownerid":
Clone_Contact[attributeName] = new EntityReference("systemuser", context.UserId);
break;
default:
Clone_Contact[attributeName] = attributeValue;
break;
}
Guid ContactId = service.Create(Clone_Contact);
Once you done with create cloned record set new GUID as entity reference to child records
Hi,
In your code, you need to create the clone of child records as well and then link.
Copying the complete code here won't be practical but you can try the following steps.
Assuming you are using a plugin or custom workflow.
1. Clone the parent record.
2. Fetch the child records using RetrievMultiple method.
3. Inside the for each loop assign or get the record to an object of type entity.
4. Set the record GUID to NULL.
5. Set Look reference to the newly cloned parent record.
6. create the child using service.create.
Hope this helps
If this helped you, please mask as verified.
Nijo
Hi,
Thanks for the reference Priyesh. It might be worth while to have a look at the packaged solution. The solution is available for trial from AppSource
Sam
Hi,
For cloning records, you'll need to actually create child records manually (or rather clone them as well) in your C#/CustomWorkflowAssembly code. Out Of The Box, there is to feature to clone the child records too.
Else, you can also evaluate products like Click2Clone by Inogic - https://www.inogic.com/blog/2016/06/click2clone-clone-your-dynamics-crm-records-in-one-click/. It's a subscription based service.
Hope this helps.
André Arnaud de Cal...
294,125
Super User 2025 Season 1
Martin Dráb
232,871
Most Valuable Professional
nmaenpaa
101,158
Moderator