Clone associated records(child records) along with parent record in CRM 2011

Hi All,
As a very common requirement, I was asked to implement the functionality to clone records in CRM 2011 just to copy the existing record and its associated entity records to the cloned one.
Since it looks very simple to clone a record using 1:N relationship mapping like explained in this blog ( but the challenge is to copy the associated child records and link them to the cloned (new) record.
So in this, the basic approach can be write a plugin on Post-Create of the cloned record, in that make a query in associated entity table and iterate through the entity collection and then map all the fields of existing child record to cloned child records.
But here the problem is to map all the fields one by one and which is quite a hectic task. Suppose there are 100 fields in 1st associated entity record,50 odd fields in 2nd associated entity record, and you are asked to clone all the fields from the associated entity then you will only end up writing 100 lines of
entity.attributes.add(“attribute_logical_name1”, “value”)
entity.attributes.add(“attribute_logical_name2”, “value”)… and so on.

Now here I tried to get rid of writing a code for every field mapping and found a tricky way. As there is no method for cloning in crmservice like Create, Execute,Update but yes we can clone the child record with the existing crmservice.Create method (see the below code).
In the below code, they main trick is to remove the Remove the primary key of the retrieved associated record, Set the primary key value to Guid.NewGuid() and finally remove the parent record id of the retrieved associated record.

public void Execute(IServiceProvider serviceProvider)
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "quote")
if (entity.Attributes.Contains("new_parentquoteid") == false)
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

EntityReference parentQuoteId = (EntityReference)entity.Attributes[“new_parentquoteid”];
Guid quoteid = new Guid(context.OutputParameters[“id”].ToString());
QueryExpression query = new QueryExpression
EntityName = “quotedetail”,
ColumnSet = new ColumnSet(true),
Criteria =
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression
AttributeName = “quoteid”,
Operator = ConditionOperator.Equal,
Values = {parentQuoteId.Id.ToString()}
//Instead of iterating through all the child records and creating one by one we can also use ExecuteMultiple method
foreach (Entity retrieve in service.RetrieveMultiple(query).Entities)
//Remove the primary key of the retrieved associated record
//Set the primary key value to Guid.NewGuid()
retrieve.Id = Guid.NewGuid();

//Remove the parent record id of the retrieved associated record

EntityReference newQuoteId = SetRefrence(“quotedetail”, quoteid);
retrieve.Attributes.Add(“quoteid”, newQuoteId);
catch (Exception ex)
throw new InvalidPluginExecutionException(ex.Message.ToString());
public static EntityReference SetRefrence(string Ent, Guid entRef)
EntityReference r_EntRef = null;
if (entRef != null) r_EntRef = new EntityReference(Ent, entRef);

return r_EntRef;

Using this, we can easily clone the child records without using any field mapping.

If you have any thoughts, doubts or concerns, kindly share with us.

Namaste 🙂



5 thoughts on “Clone associated records(child records) along with parent record in CRM 2011

    • Hi Chirag,

      I am not sure what is the exact problem but from the message I can say that your parent record in deactivate mode.
      For example,while cloning the record the particular record is getting deactivated or readonly using plugin or workflow and due to that you are not able to create child/associated records.

      So please check if there is such automation that need to tackle in this case.


  1. Hello Sir,

    I have this line… EntityReference newQuoteId = SetRefrence(“quotedetail”, quoteid);

    confrigured to my needs…

    EntityReference newkd_LeadId = SetRefrence(“lead”, kd_leadid);

    but i am recieving ” The name ‘kd_leadid’ does not exist in the current context.
    Would you be able to give me any pointers on what I can check or look into? Everything is spelt correctly. I am using CRM2016 but as far as i can tell the functions are still valid.

    Many thanks for any help.

  2. Hi Hughes,

    Please check if kd_leadid is locally declared in the method. This is the record id from where you are performing the clone.In your case it might be the leadid.

    So just check if you have kd_leadid variable declared in your method
    Guid kd_leadid = new Guid(context.OutputParameters[“id”].ToString());

    • Sir, thank you very much for this post and your quick reply to me. All working now, I had the variable set incorrectly and your prompt pointed me directly towards that.
      Much Apprechiated, have a great weekend!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s