Click here to Skip to main content
15,900,907 members
Articles / Programming Languages / C#
Tip/Trick

Dynamic CRM 365 Copy/Clone child records from One Entity to another Entity

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 May 2024CPOL2 min read 3.3K  
A plugin Copy/Clone existing child records from one Entity to another in Dynamic CRM without doing field mapping.
This is the Perfect Solution to Copy/Clone Microsoft Dynamics 365 CRM child records. This method avoids repetitive data entry by copying existing records from one entity to another entity.

Introduction

I need to clone/copy the records of the dynamic CRM entity and all its related child records. I can clone the primary entity easily by adding relationships and mapping the source entity's fields with the target entity's fields. Still, the challenge was copying/cloning its child records and linking them.

Background

You need to be aware of Dynamic CRM plugin or custom workflow development (Code Activity).
To copy or clone child records from one entity to another in Dynamics 365 CRM; you will typically need to use custom code to automate the process. The process involves querying the child records related to a specific record in one entity, creating new records in the target entity, and establishing the relationships between them.

The problem is mapping all the fields individually, which is quite hectic. Suppose there are 100 fields in the associated entity record, and you want to clone all the fields from the related entity. Then, you will only end up writing 100 lines. Also, you must update the plugin code whenever you add new fields.

I tried to eliminate writing code for every field mapping and found a tricky solution in MeghShyam Gaur's post here. The main trick is to remove the primary key of the retrieved associated record and set the primary key value to Guid.NewGuid(), and finally, remove the parent record ID of the retrieved associated record.

Using the code

  1. Identify Source and Target Entities:

    Determine the source entityId (where the child records exist) and the target entityId (where you want to copy the child records).

    C#
    //In the below example I am going to clone all contacts from one account to another account
    CloneEntityassociatedRecordsToAnother(orgService, sourceEntityId,  targetEntityId,"parentcustomerid","contact", "contactid");
  2. Query the Child Records

    The following steps are to create the clone of child records from one entity to another. Assuming you are using a plugin or custom workflow.

    C#
    public void CloneEntityassociatedRecordsToAnother(IOrganizationService organizationService,Guid sourceEntityId, Guid targetEntityId, string strForignKeyAttributeName, string strDetailEentityLogicalName, string strDetailEntityPrimaryKeyName)
    {
        try
        {
            QueryExpression query = new QueryExpression{ EntityName = strDetailEentityLogicalName, ColumnSet = new ColumnSet(true),
                Criteria = {FilterOperator = LogicalOperator.And, Conditions = {
                        new ConditionExpression {
                                AttributeName = strForignKeyAttributeName,
                                Operator = ConditionOperator.Equal,
                                Values = { sourceEntityId.ToString()}}
                    }}
            };
    
            //Instead of iterating through all the child records and creating one by one we can also use ExecuteMultiple method
            foreach (Entity retrieve in organizationService.RetrieveMultiple(query).Entities)
            {
                //Remove the primary key of the retrieved associated record
                retrieve.Attributes.Remove(strDetailEntityPrimaryKeyName);
    
                //Set the primary key value to Guid.NewGuid()
                retrieve.Id = Guid.NewGuid();
                
                //Remove the parent record id of the retrieved associated record
                retrieve.Attributes.Remove(strForignKeyAttributeName);
    
                EntityReference newContractId = SetRefrence(strDetailEentityLogicalName, targetEntityId);
                retrieve.Attributes.Add(strForignKeyAttributeName, newContractId);
                organizationService.Create(retrieve);
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException("Clone Entityassociated Records To Another Plugin: " + ex.Message);
        }
    }
  3. Establish a relationship between the newly created records and the target entity.

    C#
    public static EntityReference SetRefrence(string Ent, Guid entRef)
    {
        EntityReference r_EntRef = null;
        if (entRef != null) r_EntRef = new EntityReference(Ent, entRef);
    
        return r_EntRef;
    }
    

Points of Interest

  • Error Handling: Implement proper error handling to manage any issues arising during copying.
  • Testing: Thoroughly test your solution in a development environment before deploying it to production.

References

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --