Merge completed Email activities from Subordinate cases to Master Case – Dynamics CRM Case merging.
Views (987)
Merging Cases feature was introduced with spring ’14 update for CRM online instances and CRM 2013 Service Pack 1 for on-premises instances. By this, we can merge up to 10 cases.
Child cases state will be changed to cancel and the status is changed to merge on merging cases and also all of the open case activities, emails, and attachments of Chile cases will be associated with Master case.
But, completed emails are not moving from child case to parent instead only failed and draft emails are moving.
I have implemented this logic to move completed emails also to Parent case. PFB steps:
1. Create a Plug-in
2. Registered on “Merge” message
Plug-in Code:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(pluginContext.UserId);
if (pluginContext.InputParameters.Contains("Target") && pluginContext.InputParameters["Target"] is EntityReference)
{
EntityReference entityMaster = (EntityReference)pluginContext.InputParameters["Target"];
//Get child record guid Id
Guid guidSubOrdinate = (Guid)pluginContext.InputParameters["SubordinateId"];
tracingService.Trace("Parent Caseid :: " + entityMaster.Id + " Child Caseid:: " + guidSubOrdinate);
EntityCollection emailcol = retrieveEmails(guidSubOrdinate);
if (emailcol.Entities != null)
{
foreach (Entity email in emailcol.Entities)
{
int statecode = ((OptionSetValue)email[STATECODE_ATTRIBUTENAME]).Value;
int statuscode = ((OptionSetValue)email[STATUSCODE_ATTRIBUTENAME]).Value;
//email Status is opened if it is completed or canceled
if (statecode != 0)
{
SetStateRequest setStateRequest = new SetStateRequest
{
EntityMoniker = new EntityReference(email.LogicalName, email.Id),
State = new OptionSetValue(0),
Status = new OptionSetValue(1),
};
service.Execute(setStateRequest);
//email regarding updated with parent case
email[REGARDINGOBJECTID_ATTRIBUTENAME] = new EntityReference(entityMaster.LogicalName, entityMaster.Id);
service.Update(email);
}
else
{
//email regarding updated with parent case
email[REGARDINGOBJECTID_ATTRIBUTENAME] = new EntityReference(entityMaster.LogicalName, entityMaster.Id);
service.Update(email);
}
//email status updated as per previous status
if (statecode != 0)
{
SetStateRequest setStateRequest = new SetStateRequest
{
EntityMoniker = new EntityReference(email.LogicalName, email.Id),
State = new OptionSetValue(statecode),
Status = new OptionSetValue(statuscode),
};
service.Execute(setStateRequest);
}
}
}
}
}
//Retrieve all emails which are having regarding child case
public EntityCollection retrieveEmails(Guid id)
{
ConditionExpression condition1 = new ConditionExpression(REGARDINGOBJECTID_ATTRIBUTENAME, ConditionOperator.Equal, id);
FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition1);
QueryExpression query = new QueryExpression(EMAIL_ENTITYNAME);
query.ColumnSet.AddColumns(REGARDINGOBJECTID_ATTRIBUTENAME, STATECODE_ATTRIBUTENAME, STATUSCODE_ATTRIBUTENAME);
query.Criteria.AddFilter(filter1);
EntityCollection result1 = service.RetrieveMultiple(query);
if (result1.Entities.Count != 0)
return result1;
else
return null;
}
This was originally posted here.

Like
Report
*This post is locked for comments