Today came up with a task regarding how we can deactivate related child records once parent record is getting deactivated.Below Screenshot shared :
Here My Requirement is : In Account entity i have lookup field with Event Entity(Parent Record).
Once Event record gets deactivated then related child records(Account Record) associated with Event entity must be deactivated.
I have done using c# plugin code.
1. Please find the below code :
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
{
var entity = (EntityReference)context.InputParameters["EntityMoniker"];
var state = (OptionSetValue)context.InputParameters["State"];
var status = (OptionSetValue)context.InputParameters["Status"];
if(entity.LogicalName=="ags_event" && state.Value==1)
{
QueryExpression account = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("accountid", "ags_event") };
account.Criteria.AddCondition("ags_event", ConditionOperator.Equal, entity.Id);
account.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection retrieveAccount = service.RetrieveMultiple(account);
if (retrieveAccount.Entities.Count > 0)
{
foreach (var a in retrieveAccount.Entities)
{
SetStateRequest accountRequest = new SetStateRequest();
accountRequest.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
accountRequest.State = new OptionSetValue(1);
accountRequest.Status = new OptionSetValue(-1);
service.Execute(accountRequest);
}
}
}
if (state.Value == 0)
{
QueryExpression account = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("accountid", "ags_event") };
account.Criteria.AddCondition("ags_event", ConditionOperator.Equal, entity.Id);
account.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
EntityCollection retrieveAccount = service.RetrieveMultiple(account);
if (retrieveAccount.Entities.Count > 0)
{
foreach (var a in retrieveAccount.Entities)
{
SetStateRequest accountRequest = new SetStateRequest();
accountRequest.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
accountRequest.State = new OptionSetValue(0);
accountRequest.Status = new OptionSetValue(-1);
service.Execute(accountRequest);
}
}
}
}
}
2. Register this c# code on setStateDynamicEntity Message using Plugin Registration Tool.
Hope it would be helpful to someone !!
Here My Requirement is : In Account entity i have lookup field with Event Entity(Parent Record).
Once Event record gets deactivated then related child records(Account Record) associated with Event entity must be deactivated.
I have done using c# plugin code.
1. Please find the below code :
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
{
var entity = (EntityReference)context.InputParameters["EntityMoniker"];
var state = (OptionSetValue)context.InputParameters["State"];
var status = (OptionSetValue)context.InputParameters["Status"];
if(entity.LogicalName=="ags_event" && state.Value==1)
{
QueryExpression account = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("accountid", "ags_event") };
account.Criteria.AddCondition("ags_event", ConditionOperator.Equal, entity.Id);
account.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection retrieveAccount = service.RetrieveMultiple(account);
if (retrieveAccount.Entities.Count > 0)
{
foreach (var a in retrieveAccount.Entities)
{
SetStateRequest accountRequest = new SetStateRequest();
accountRequest.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
accountRequest.State = new OptionSetValue(1);
accountRequest.Status = new OptionSetValue(-1);
service.Execute(accountRequest);
}
}
}
if (state.Value == 0)
{
QueryExpression account = new QueryExpression { EntityName = "account", ColumnSet = new ColumnSet("accountid", "ags_event") };
account.Criteria.AddCondition("ags_event", ConditionOperator.Equal, entity.Id);
account.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
EntityCollection retrieveAccount = service.RetrieveMultiple(account);
if (retrieveAccount.Entities.Count > 0)
{
foreach (var a in retrieveAccount.Entities)
{
SetStateRequest accountRequest = new SetStateRequest();
accountRequest.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
accountRequest.State = new OptionSetValue(0);
accountRequest.Status = new OptionSetValue(-1);
service.Execute(accountRequest);
}
}
}
}
}
2. Register this c# code on setStateDynamicEntity Message using Plugin Registration Tool.
Hope it would be helpful to someone !!
No comments:
Post a Comment