Introduction

This blog explain about How To Deactivate Child Records Using Plug-In.

On the basis of the Parent record’s deactivation or activation, we may occasionally desire to deactivate or activate records. We can fulfil this requirement by creating a plugin for the parent entity update that allows us to determine whether the statecode value is 0 or 1 (most activities have two states: 0- Active and 1- Inactive), though we can also check for additional statecodes.

IOrganizationService service = null;

public void Execute(IServiceProvider serviceProvider)
{

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = serviceFactory.CreateOrganizationService(context.UserId);

Entity primaryEntity = null;

if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
try
{
primaryEntity = (Entity)context.InputParameters[“Target”];

//check if record is deactivated
if (primaryEntity.Contains(“statecode”) &&
primaryEntity.GetAttributeValue<OptionSetValue>(“statecode”).Value == 1)
{
//for disable send operation as 0
EnableDisableOneToManyRelatedEntities(“childentitylogicalname”, “parententitylookupfieldlogicalname”, primaryEntity.Id,0);
}
//check if record is activated
else if (primaryEntity.Contains(“statecode”) &&
primaryEntity.GetAttributeValue<OptionSetValue>(“statecode”).Value == 0)
{
//for disable send operation as 1
EnableDisableOneToManyRelatedEntities(“childentitylogicalname”, “parententitylookupfieldlogicalname”, primaryEntity.Id,1);
}
}
catch (Exception ex)
{
tracingService.Trace(“Error occured while deactivating “+primaryEntity.LogicalName +” child records Detials” + ex.Message);
}
}
}

In the above code we are retrieving primary entity from the context and checking statecode value. Based on the satecode value we are calling another method to perform activation and deactivation like below.

private void EnableDisableOneToManyRelatedEntities(string entityname, string parententityfieldname, Guid parentfieldvalue,int operation)
{
EntityCollection results = null;
QueryExpression query = new QueryExpression()
{
EntityName = entityname,
Criteria =
{
FilterOperator = LogicalOperator.And,
Filters =
{
new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression(parententityfieldname,ConditionOperator.Equal,parentfieldvalue),
new ConditionExpression(“statecode”,ConditionOperator.Equal,operation)
},
}

}
}
};
results = service.RetrieveMultiple(query);

#region Disable records
if (operation == 0)
{
//deactivate records
foreach (Entity relatedentity in results.Entities)
{
Entity deactivateRecord = new Entity(entityname);
deactivateRecord.Id = relatedentity.Id;
deactivateRecord[“statecode”] = new OptionSetValue(1);
deactivateRecord[“statuscode”] = new OptionSetValue(2);

service.Update(deactivateRecord);

}
}
#endregion
#region Enable records
else if (operation==1)
{
//activate them
foreach (Entity relatedentity in results.Entities)
{
Entity activateRecord = new Entity(entityname);
activateRecord.Id = relatedentity.Id;
activateRecord[“statecode”] = new OptionSetValue(0);
activateRecord[“statuscode”] = new OptionSetValue(1);

service.Update(activateRecord);

}
}
#endregion
}

According to the operation, we retrieve data from the child entity using the aforementioned manner. For instance, if the parent record is deactivated, we fetch the child entity records that are linked to it and in the activate state, and after we have them, we deactivate these records. To ensure that this plugin only activates when status changes, we can register it on the parent entity’s update and choose statecode under the Filtering Attribute.

For any Help or Queries Contact us on info@crmonce.com or +918096556344