This is an OOB behavior, i.e. when you try to change the status reason of deactivated account, it will clear both the field “masterid” and “merged” using an OOB workflow.
Now simpler approach will be to change the default status reason of Status Reason field to “Merged” as when a merge event gets triggered it will select the default status reason against “Inactive” status. But using this approach may also affect normal deactivated accounts as well so using this approach is not feasible.
So writing a plugin on merge message can retain the value of masterid and merged flag.
Here is the sample code for the same.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
EntityReference masterEntity = (EntityReference)context.InputParameters["Target"];
Guid guidSubOrdinate = (Guid)context.InputParameters["SubordinateId"];
// Get Master record information. Specify the attributes which you want to retrieve
Entity entMasterAccount = service.Retrieve("account", masterEntity.Id,new ColumnSet("name","accountid"));
// Get Subordinate record information. Specify the attributes which you want to retrieve
Entity entSubOrdinateAccount = service.Retrieve("account", guidSubOrdinate, new ColumnSet(new string[] { "statecode", "statuscode", "merged", "masterid" }));
if(entSubOrdinateAccount!=null)
{
Entity mergedAcc = new Entity(entSubOrdinateAccount.LogicalName, entSubOrdinateAccount.Id);
//Change Status Reason
if(entSubOrdinateAccount.Contains("statuscode") && entSubOrdinateAccount["statuscode"]!=null)
{
mergedAcc["statuscode"] = new OptionSetValue(100000000);
}
//Re-Set Master Id
if(entSubOrdinateAccount.Contains("masterid") && entSubOrdinateAccount["masterid"]!=null)
{
mergedAcc["masterid"] = new EntityReference(entMasterAccount.LogicalName, entMasterAccount.Id);
}
//Re-Set Merge Flag
if(entSubOrdinateAccount.Contains("merged") && entSubOrdinateAccount["merged"]!=null)
{
mergedAcc["merged"] = true;
}
if(mergedAcc!=null)
{
//Update Record.
service.Update(mergedAcc);
}
}
}
Hope this helps.
Thanks!