Skip to content

Commit

Permalink
Common.CloneRecord fieldToReplace
Browse files Browse the repository at this point in the history
Moved logic that updates parent record ID on clone into core CloneRecord logic. Fix for issue demianrasko#255.
  • Loading branch information
sressler-cargas committed Mar 8, 2022
1 parent ab9d77f commit 54c4e0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
24 changes: 11 additions & 13 deletions msdyncrmWorkflowTools/msdyncrmWorkflowTools/Class/CloneChildren.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,19 @@ protected override void Execute(CodeActivityContext executionContext)
var tools = new msdyncrmWorkflowTools_Class(objCommon.service);

var children = tools.GetChildRecords(_relationshipName, parentId);

foreach (var item in children.Entities)
{
var newRecordId = objCommon.CloneRecord(item.LogicalName, item.Id.ToString(), fieldstoIgnore, prefix);

Entity update = new Entity(item.LogicalName);
update.Id = newRecordId;
update.Attributes.Add(_newParentFieldName, new EntityReference(destinationEntityName, new Guid(destinationId)));
if (!string.IsNullOrEmpty(_oldParentFieldName) && _oldParentFieldName != _newParentFieldName)
{
update.Attributes.Add(_oldParentFieldName, null);
}

objCommon.service.Update(update);
var fieldsToReplace = new Dictionary<string, object>
{
{ _newParentFieldName, new EntityReference(destinationEntityName, new Guid(destinationId)) }
};
if (!string.IsNullOrEmpty(_oldParentFieldName) && _oldParentFieldName != _newParentFieldName)
{
fieldsToReplace.Add(_oldParentFieldName, null);
}

foreach (var item in children.Entities)
{
_ = objCommon.CloneRecord(item.LogicalName, item.Id.ToString(), fieldstoIgnore, prefix, fieldsToReplace);
}


Expand Down
18 changes: 17 additions & 1 deletion msdyncrmWorkflowTools/msdyncrmWorkflowTools/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public List<string> getEntityAttributesToClone(string entityName, IOrganizationS
return (atts);
}

public Guid CloneRecord(string entityName, string objectId, string fieldstoIgnore, string prefix)
public Guid CloneRecord(string entityName, string objectId, string fieldstoIgnore, string prefix, Dictionary<string, object> fieldsToReplace = null)
{
tracingService.Trace("entering CloneRecord");
if (fieldstoIgnore == null) fieldstoIgnore = "";
Expand Down Expand Up @@ -201,6 +201,22 @@ public Guid CloneRecord(string entityName, string objectId, string fieldstoIgnor
}
}

if (fieldsToReplace != null)
{
foreach (KeyValuePair<string, object> replaceField in fieldsToReplace)
{
tracingService.Trace("attribute:{0}", replaceField.Key);
if (newEntity.Attributes.ContainsKey(replaceField.Key))
{
newEntity[replaceField.Key] = replaceField.Value;
}
else
{
newEntity.Attributes.Add(replaceField.Key, replaceField.Value);
}
}
}

tracingService.Trace("creating cloned object...");
Guid createdGUID = service.Create(newEntity);
tracingService.Trace("created cloned object OK");
Expand Down

0 comments on commit 54c4e0d

Please sign in to comment.