Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Sales forum / How to clone record wi...
Sales forum

How to clone record with child records in custom entity

(0) ShareShare
ReportReport
Posted on by 319

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..

Categories:
  • Zap Objects Profile Picture
    919 on at
    How to clone record with child records in custom entity
    Hello,
     
    You could also try Cloning record (and its child records) using Zap Objects: Copy Record App. 
    Here is the features page link: https://zapobjects.com/apps/zap-copy-record/
    Feel free to email sales@zapobjects.com to schedule a quick demo!
     
  • Suggested answer
    Yahya79 Profile Picture
    6 on at
    How to clone record with child records in custom entity
    The complete solution can be found in the article below:
     
  • Nojoble Profile Picture
    27 on at
    RE: How to clone record with child records in custom entity

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

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,235 on at
    RE: How to clone record with child records in custom entity

    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

  • Suggested answer
    Nijos Profile Picture
    1,672 on at
    RE: How to clone record with child records in custom entity

    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

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    2,545 on at
    RE: How to clone record with child records in custom entity

    Hi,

    You can retrieve all related records of your parent record by using RetrievMultiple function and then you can create a copy of those child records by removing guid for the respective record.

    Please use below code to clone related child records. We clone related Quote Products of Quote entity in below example:

        QuoteProducts = service.RetrieveMultiple(new FetchExpression(fetchQuoteProducts));
     
                    tracingService.Trace("QuoteProductsCollectionCount: " + QuoteProducts.Entities.Count);
                    if (QuoteProducts != null) //&& QuoteProducts.TotalRecordCount > 0
                    {
                        tracingService.Trace("QuoteProducts != null");
     
                        foreach (Entity quoteLineItem in QuoteProducts.Entities)
                        {
                            Entity cloneQuoteProduct = new Entity();
                            quoteLineItem.Attributes.Remove("quoteid");
                            quoteLineItem.Attributes.Remove("quotedetailid");
                            quoteLineItem.Id = Guid.NewGuid();
                            quoteLineItem.Attributes["quoteid"] = new EntityReference("quote", cloneQuoteId);
                            cloneQuoteProduct = quoteLineItem;
                            tracingService.Trace("before quote product create");
                            service.Create(cloneQuoteProduct);
                            tracingService.Trace("after quote product create");
                        }
                    }
     
  • Inogic Profile Picture
    462 on at
    RE: How to clone record with child records in custom entity

    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

  • Suggested answer
    Priyesh Profile Picture
    7,396 User Group Leader on at
    RE: How to clone record with child records in custom entity

    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 Inogichttps://www.inogic.com/blog/2016/06/click2clone-clone-your-dynamics-crm-records-in-one-click/. It's a subscription based service.

    Hope this helps.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,125 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,871 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans