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