Copy Subgrid data from one entity to another entity


Hello, We can copy data from one entity to another using System workflow. But many times we are getting requirement of copy Subgrid from one entity to another entity.
So here is your answer...
Subgrid is nothing but the relationship between two entities. So if you want to copy the Subgrid then, you need to create relationship with the record and Subgrid records.
This can be achievable by writing custom workflow or Plugin.
Let’s take an Example as we want to Copy Subgrid of C from Entity A to Entity B.
In Entity A we have condition, on change of record "Copy Record from A to B" to "Yes", Subgrid Entity C should be copied from A to Entity B
Entity A:

Entity B:


So I am creating System workflow, in which I’ll call Custom Workflow:
Steps to follow:
Let’ first create Custom Workflow:

1. Retrieve all record of Subgrid C from entity A using relationship condition

QueryExpression query_data = new QueryExpression("new_entityc");
query_data.ColumnSet = new ColumnSet(true);
ConditionExpression condition_Data = new ConditionExpression("LookupAnameInC", ConditionOperator.Equal, RecordAGuid);
query_data.Criteria.AddCondition(condition_Data);
EntityCollection EntityData = service.RetrieveMultiple(query_data);

2. Update all the records retrieved with the Source Entity Record

Entity EntityC = new Entity("new_entityc");
            if (EntityData.Entities.Count > 0)
            {
                foreach (Entity objData in EntityData.Entities)
                {
                    var id = objData.Id;

                    EntityC.Id = id;

                    //Update the data
                    EntityC.Attributes["LookupBnameInC"] = new EntityReference("new_entityc", RecordBGuid);
                    service.Update(EntityC);
                }

            }            

Let’s Create System workflow and call Custom workflow


Now I’ll change "Copy Record from A to B" to "Yes", So here you go. My subgrid is copied from Entity A to Entity B.
You can see that Subgrid will get copied....


Do let us know if this is useful for you.....

Till then... Happy CRM Surfing...

Comments

Popular posts from this blog

Explaining Attachment Management Solution for Dynamics CRM/365 Online

Take Signature in CRM Application