Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / How to clone record wi...
Sales forum
Suggested answer

How to clone record with child records in custom entity

Posted on by 315

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
    Zap Objects 909 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
    Yahya79 2 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
    Nojoble 25 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
    Pawar Pravin 5,225 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
    Nijos 1,670 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
    Kalpavruksh D365 CoE 2,543 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
    Inogic 23,963 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
    Priyesh 7,363 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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,835 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,526 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans