Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common.CloneRecord fieldToReplace #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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