以编程方式完成条件业务流程时引发的异常

时间:2018-09-28 16:04:09

标签: c# .net dynamics-crm dynamics-crm-365

下面是商机实体上的业务流程(BPF)。我创建了一个插件,该插件会自动将BPF推进到商机字段更新的最后阶段。

bpf

当为满足上述条件的机会发送RetrieveActivePathRequest消息(并因此利用了BPF的所有阶段)时,我收到RetrieveActivePathResponse,并且我的插件按预期工作:

        // Retrieve the process stages in the active path of the current process instance
        RetrieveActivePathRequest activePathRequest = new RetrieveActivePathRequest
        {
            ProcessInstanceId = activeProcessId
        };

        RetrieveActivePathResponse activePathResponse = (RetrieveActivePathResponse)service.Execute(activePathRequest);

但是,为不满足条件的机会发送RetrieveActivePathRequest消息(因而仅利用BPF的前三个阶段)会导致 System.ServiceModel.FaultException

我怀疑这可能是由于BPF中的“ else”条件未分支到某个阶段所致。我添加了一个共享阶段,并将其与“ else”条件和上图所示的最后一个阶段联系在一起,不再遇到异常。我的插件按预期运行,并自动提高了BPF。

两个问题:

  1. 为什么会这样?在这两种情况下,我都可以手动完成每个阶段并完成BPF,所以为什么不能以编程方式执行相同的操作?

  2. 是否有另一种方法来处理这种情况,所以我可以 通过插件自动推进BPF(按原样)?如果可能的话,我宁愿不创建共享阶段或两个单独的BPF。

编辑:

如果有人好奇,这是我用来自动推进BPF的全部功能。

    private static void AdvanceBusinessProcessFlow(IOrganizationService service, ExtendedPluginContext context, Guid opportunityId)
    {
        // Retrieve all process instances
        RetrieveProcessInstancesRequest instanceRequest = new RetrieveProcessInstancesRequest
        {
            EntityId = opportunityId,
            EntityLogicalName = XrmOpportunity.EntityLogicalName
        };
        RetrieveProcessInstancesResponse instanceResponse = (RetrieveProcessInstancesResponse)service.Execute(instanceRequest);

        // First record is the active process instance
        Entity activeProcessInstance = instanceResponse.Processes.Entities[0];
        var activeProcessId = activeProcessInstance.Id;
        var activeStageId = new Guid(activeProcessInstance.Attributes["processstageid"].ToString());

        // Retrieve the process stages in the active path of the current process instance
        RetrieveActivePathRequest activePathRequest = new RetrieveActivePathRequest
        {
            ProcessInstanceId = activeProcessId
        };

        // System.ServiceModel.FaultException exception occurs here
        RetrieveActivePathResponse activePathResponse = (RetrieveActivePathResponse)service.Execute(activePathRequest);

        string activeStageName;
        int? activeStagePosition = null;
        int stageCount = activePathResponse.ProcessStages.Entities.Count;

        // Iterate through all process stages and identify active stage
        for (int i = 0; i < stageCount; i++)
        {
            if (activePathResponse.ProcessStages.Entities[i].Attributes["processstageid"].ToString() == activeStageId.ToString())
            {
                activeStageName = activePathResponse.ProcessStages.Entities[i].Attributes["stagename"].ToString();
                activeStagePosition = i;
            }
        }

        // If an active stage position is not identified, do nothing
        if (activeStagePosition == null)
        {
            throw new InvalidPluginExecutionException("No active stage of business process flow was identified!");
        }

        // Auto-advance active stages of BPF to last stage so that BPF can be auto-finished
        while (activeStagePosition < stageCount - 1)
        {
            // Retrieve the stage ID of the next stage to be set as the active stage
            var newActiveStageId = (Guid) activePathResponse.ProcessStages.Entities[(int) ++activeStagePosition].Attributes["processstageid"];

            // Retrieve the process instance record to update its active stage
            ColumnSet columnSet = new ColumnSet();
            columnSet.AddColumn("activestageid");
            Entity retrievedProcessInstance = service.Retrieve(dfnd_opportunitydispoprocess.EntityLogicalName, activeProcessId, columnSet);

            // Update active process stage
            retrievedProcessInstance["activestageid"] = new EntityReference(ProcessStage.EntityLogicalName, newActiveStageId);
            service.Update(retrievedProcessInstance);
        }
    }

1 个答案:

答案 0 :(得分:0)

定向到不满足BPF条件的阶段可防止引发该错误:

bpf2

我仍然不知道为什么对于不满足BPF条件的商机,不会返回RetrieveActivePathResponse对象。