如何删除业务规则引用的 CRM 属性

How to Delete a CRM Attribute that is referenced by Business Rule

我正在尝试从 CRM online 2015 Sp1 中的实体中删除在业务规则中引用的属性。我是 运行 RetrieveDependenciesForDeleteRequest 以获取引用该字段的工作流,然后我将 XAML 中的字段名称更新为不同的字段,首先禁用工作流,然后更新工作流,激活它。无论我做什么,我似乎都无法在 Workflow/Business 规则中删除此字段的依赖项。

这就是我目前的逻辑:

private void UpdateWorkflows(IOrganizationService service, AttributeMetadata from, AttributeMetadata to)
{
    Trace("Checking for Workflow Dependencies");
    var depends = ((RetrieveDependenciesForDeleteResponse)service.Execute(new RetrieveDependenciesForDeleteRequest
    {
        ComponentType = (int)componenttype.Attribute,
        ObjectId = from.MetadataId.GetValueOrDefault()
    })).EntityCollection.ToEntityList<Dependency>().Where(d => d.DependentComponentTypeEnum == componenttype.Workflow).ToList();

    if (!depends.Any())
    {
        Trace("No Workflow Dependencies Found");
        return;
    }

    foreach (var workflow in service.GetEntitiesById<Workflow>(depends.Select(d => d.DependentComponentObjectId.GetValueOrDefault())))
    {
        Trace("Updating {0} - {1} ({2})", workflow.CategoryEnum.ToString(), workflow.Name, workflow.Id);
        workflow.Xaml = workflow.Xaml.Replace("\"" + from.LogicalName + "\"", "\"" + to.LogicalName + "\"");
        var activate = workflow.StateCode.Value == WorkflowState.Activated;
        if (activate)
        {
            service.Execute(new SetStateRequest()
            {
                EntityMoniker = workflow.ToEntityReference(),
                State = new OptionSetValue((int) WorkflowState.Draft),
                Status = new OptionSetValue((int) workflow_statuscode.Draft)
            });
        }
        service.Update(workflow);
        if (activate)
        {
            service.Execute(new SetStateRequest()
            {
                EntityMoniker = workflow.ToEntityReference(),
                State = new OptionSetValue((int)WorkflowState.Activated),
                Status = new OptionSetValue((int)workflow_statuscode.Activated)
            });
        }
    }
}

终于想通了!还有一些流程触发器也是使用业务规则创建的:

添加了这个额外的步骤,现在一切正常!

var triggers = service.GetEntities<ProcessTrigger>(ProcessTrigger.Fields.ProcessId, workflow.Id, 
                                                   ProcessTrigger.Fields.ControlName, from.LogicalName);
if (triggers.Count > 0)
{
    foreach (var trigger in triggers)
    {
        Trace("Updating Trigger {0} for Workflow", trigger.Id);
        service.Update(new ProcessTrigger
        {
            Id = trigger.Id,
            ControlName = to.LogicalName
        });
    }
}