Hi ,
I am trying to restrict the closing of parent case when a associated child case is open . I am aware that this can be done using parent child case setting in service management. But due to some requirement issue i am not using it. So for this i am trying to write a pluign . I tried two approaches :-
1) First i wrote a plugin at close message of incident entity. Yet somehow , this pluign is never getting triggered.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CloseCase
{
public class ParentCase : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService
(typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
QueryExpression qe2 = new QueryExpression("incident");
ColumnSet col2 = new ColumnSet("parentcaseid");
qe2.Criteria.AddCondition("parentcaseid", ConditionOperator.Equal, entity.Id);
qe2.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1);
EntityCollection coll = service.RetrieveMultiple(qe2);
if (coll.Entities.Count > 0)
{
throw new Exception("child record not closed");
}
}
}
}
}
2) In 2nd approach i wrote a plugin on create of incidentResolution entity , and i am able to achieve my desired functionality i.e restricting the closing of parent case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CloseCase
{
public class ParentCase : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService
(typeof(IOrganizationServiceFactory));
IOrganizationService service =
serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
QueryExpression qe2 = new QueryExpression("incident");
ColumnSet col2 = new ColumnSet("parentcaseid");
qe2.Criteria.AddCondition("parentcaseid", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("incidentid").Id);
qe2.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1);
EntityCollection coll = service.RetrieveMultiple(qe2);
if (coll.Entities.Count > 0)
{
throw new Exception("child record not closed");
}
}
}
}
}
But somehow i am confused why my 1st approach is not working , maybe the code is not right but at least it should hit the execute method. I am not getting it , that on resolving the case , why close event is not triggering.
Please help me to understand if i am missing something.
Thanks