Hi Gordon,
You can write a plugin to delete the waiting workflows like below and register it on the pre-operation stage and delete message of the particular entity.
public class RemoveRunningWorkflows : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
OrganizationServiceContext serviceContext = new OrganizationServiceContext(service);
if (context.MessageName.ToLower() == "delete")
{
EntityReference entity = (EntityReference)context.InputParameters["Target"];
QueryExpression query = new QueryExpression("asyncoperation");
ConditionExpression entityExpression = new ConditionExpression("regardingobjectid", ConditionOperator.Equal, entity.Id);
ConditionExpression statusExpression = new ConditionExpression("statuscode", ConditionOperator.Equal, 10);//waiting
FilterExpression filter = new FilterExpression(LogicalOperator.And);
filter.AddCondition(entityExpression);
filter.AddCondition(statusExpression);
query.Criteria.AddFilter(filter);
var systemJobs = service.RetrieveMultiple(query);
foreach (var workflow in systemJobs.Entities)
{
service.Delete("asyncoperation", workflow.Id);
}
}
}
}
Thanks
Sachith
Sachith Chandrasiri