In my earlier post I have already written about cascade deactivate records using plugin.But this article is for deactivating child records once parent record gets deactivated.
Here I have Account lookup field present on Event Entity form. Once account record gets deactivated then it should deactivate related event record.Below screenshot follows :

1. Below code I wrote :
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(state.Value==1)
{
QueryExpression event= new QueryExpression { EntityName = "ags_event", ColumnSet = new ColumnSet(true) };
event.Criteria.AddCondition("ags_account", ConditionOperator.Equal, entity.Id);
event.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection retrieveEvent = service.RetrieveMultiple(event);
if (retrieveEvent.Entities.Count > 0)
{
foreach (var a in retrieveEvent.Entities)
{
SetStateRequest request = new SetStateRequest();
request .EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
request .State = new OptionSetValue(1);
request .Status = new OptionSetValue(-1);
service.Execute(request );
}
}
}
if (state.Value == 0)
{
QueryExpression event= new QueryExpression { EntityName = "ags_event", ColumnSet = new ColumnSet(true) };
event.Criteria.AddCondition("ags_account", ConditionOperator.Equal, entity.Id);
event.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
EntityCollection retrieveEvent = service.RetrieveMultiple(event);
if (retrieveEvent.Entities.Count > 0)
{
foreach (var a in retrieveEvent.Entities)
{
SetStateRequest request = new SetStateRequest();
request .EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
request .State = new OptionSetValue(0);
request.Status = new OptionSetValue(-1);
service.Execute(request );
}
}
}
}
}
2. Register the plugin With message SetStateDynamicEntity on Account Entity.
Below Screenshots You can see :


Hope it may helps !!
Here I have Account lookup field present on Event Entity form. Once account record gets deactivated then it should deactivate related event record.Below screenshot follows :

1. Below code I wrote :
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(state.Value==1)
{
QueryExpression event= new QueryExpression { EntityName = "ags_event", ColumnSet = new ColumnSet(true) };
event.Criteria.AddCondition("ags_account", ConditionOperator.Equal, entity.Id);
event.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection retrieveEvent = service.RetrieveMultiple(event);
if (retrieveEvent.Entities.Count > 0)
{
foreach (var a in retrieveEvent.Entities)
{
SetStateRequest request = new SetStateRequest();
request .EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
request .State = new OptionSetValue(1);
request .Status = new OptionSetValue(-1);
service.Execute(request );
}
}
}
if (state.Value == 0)
{
QueryExpression event= new QueryExpression { EntityName = "ags_event", ColumnSet = new ColumnSet(true) };
event.Criteria.AddCondition("ags_account", ConditionOperator.Equal, entity.Id);
event.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
EntityCollection retrieveEvent = service.RetrieveMultiple(event);
if (retrieveEvent.Entities.Count > 0)
{
foreach (var a in retrieveEvent.Entities)
{
SetStateRequest request = new SetStateRequest();
request .EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
request .State = new OptionSetValue(0);
request.Status = new OptionSetValue(-1);
service.Execute(request );
}
}
}
}
}
2. Register the plugin With message SetStateDynamicEntity on Account Entity.

Below Screenshots You can see :


Hope it may helps !!