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

时间:2016-02-03 20:30:20

标签: c# dynamics-crm dynamics-crm-online

我试图从业务规则中引用的CRM online 2015 Sp1中的实体中删除属性。我正在运行RetrieveDependenciesForDeleteRequest以获取引用该字段的工作流,然后我将XAML中字段的名称更新为另一个字段,首先取消激活工作流,然后更新工作流,激活它。无论我做什么,我似乎无法在工作流程/业务规则中删除此字段的依赖关系。

这就是我目前的逻辑:

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)
            });
        }
    }
}

1 个答案:

答案 0 :(得分:0)

终于想出这个了!还有使用业务规则创建的流程触发器:

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

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
        });
    }
}