gpt4 book ai didi

c# - 在 C# 插件中更改业务流程阶段

转载 作者:太空狗 更新时间:2023-10-29 19:47:40 32 4
gpt4 key购买 nike

我正在关注 this在 c# 插件中更改我的业务流程阶段的文章。我能够将阶段向前移动到下一个阶段,但是当我尝试返回到上一个阶段时收到错误消息。下面的错误是我在 UI 中从 Dynamics 收到的错误。当我调试插件时,我收到一个 FaultException<OrganizationServiceFault>不包含任何信息的异常。为什么我会收到错误以及如何修改我的代码以成功返回到我的业务流程中的前一个阶段?

错误

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An unexpected error occurred.
Detail: <OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ActivityId>5df51362-b7c1-4817-a8d0-de2d63b15c17</ActivityId>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<Message>An unexpected error occurred.</Message>
<Timestamp>2018-07-19T18:55:42.6625925Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault>
<ActivityId>5df51362-b7c1-4817-a8d0-de2d63b15c17</ActivityId>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
<Message>System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0D309052</Message>
<Timestamp>2018-07-19T18:55:42.6625925Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</InnerFault>
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</OrganizationServiceFault>

插件

if (localContext == null)
{
throw new ArgumentNullException("localContext");
}

IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;

Client client = (Client)service.Retrieve(
Client.LogicalName,
new Guid("75FE165F-848B-E811-80F3-005056B33317"),
new ColumnSet(new String[]{
Client.Properties.ClientId
})
);

client.ChangeStage(service);

改变阶段

public void ChangeStage(IOrganizationService service)
{
// Get Process Instances
RetrieveProcessInstancesRequest processInstanceRequest = new RetrieveProcessInstancesRequest
{
EntityId = this.Id,
EntityLogicalName = this.LogicalName
};

RetrieveProcessInstancesResponse processInstanceResponse = (RetrieveProcessInstancesResponse)service.Execute(processInstanceRequest);

// Declare variables to store values returned in response
int processCount = processInstanceResponse.Processes.Entities.Count;
Entity activeProcessInstance = processInstanceResponse.Processes.Entities[0]; // First record is the active process instance
Guid activeProcessInstanceID = activeProcessInstance.Id; // Id of the active process instance, which will be used later to retrieve the active path of the process instance

// Retrieve the active stage ID of in the active process instance
Guid activeStageID = new Guid(activeProcessInstance.Attributes["processstageid"].ToString());

// Retrieve the process stages in the active path of the current process instance
RetrieveActivePathRequest pathReq = new RetrieveActivePathRequest
{
ProcessInstanceId = activeProcessInstanceID
};
RetrieveActivePathResponse pathResp = (RetrieveActivePathResponse)service.Execute(pathReq);

string activeStageName = "";
int activeStagePosition = -1;

Console.WriteLine("\nRetrieved stages in the active path of the process instance:");
for (int i = 0; i < pathResp.ProcessStages.Entities.Count; i++)
{
// Retrieve the active stage name and active stage position based on the activeStageId for the process instance
if (pathResp.ProcessStages.Entities[i].Attributes["processstageid"].ToString() == activeStageID.ToString())
{
activeStageName = pathResp.ProcessStages.Entities[i].Attributes["stagename"].ToString();
activeStagePosition = i;
}
}

// Retrieve the stage ID of the next stage that you want to set as active
activeStageID = (Guid)pathResp.ProcessStages.Entities[activeStagePosition - 1].Attributes["processstageid"];

// Retrieve the process instance record to update its active stage
ColumnSet cols1 = new ColumnSet();
cols1.AddColumn("activestageid");
Entity retrievedProcessInstance = service.Retrieve("ccseq_bpf_clientsetup", activeProcessInstanceID, cols1);

// Set the next stage as the active stage
retrievedProcessInstance["activestageid"] = new EntityReference(ProcessStage.LogicalName, activeStageID);
service.Update(retrievedProcessInstance);
}

更新

我找到了 this介绍如何使用 Web API 更新舞台的文章。当我尝试这种方法时,出现错误:

An undeclared property 'activestageid' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.

我尝试了几种“activestageid”但没有成功(ActiveStageId、_activestageid_value)。


更新2

根据 Arun 的反馈,我尝试了以下 Web API 调用但没有成功。我从 ccseq_bpf_clientsetups 表上的 BusinessProcessFlowInstanceId 中提取的 url (ccseq_bpf_clientsetups(###)) 中括号内的 ID。我从 ProcessStageBase 表中的 ProcessStageId 中提取的 processstages ID

// Attempt 1
PATCH /COHEN/api/data/v8.2/ccseq_bpf_clientsetups(bc892aec-2594-e811-80f4-005056b33317) HTTP/1.1
{ "ActiveStageID@odata.bind": "/processstages(70018854-db7c-4612-915b-2ad7870a8574)"}

// Attempt 2
PATCH /COHEN/api/data/v8.2/ccseq_bpf_clientsetups(bc892aec-2594-e811-80f4-005056b33317) HTTP/1.1
{ "activestageid@odata.bind": "/processstages(70018854-db7c-4612-915b-2ad7870a8574)"}

// Attempt 3
PATCH /COHEN/api/data/v8.2/ccseq_bpf_clientsetups(bc892aec-2594-e811-80f4-005056b33317) HTTP/1.1
{ "ActiveStageId@odata.bind": "/processstages(70018854-db7c-4612-915b-2ad7870a8574)"}

更新3

我下载了 jLattimer 的 CRM Rest Builder 并尝试运行他的工具生成的 JavaScript。该代码与我之前编写的代码相同,不幸的是没有用。在这一点上,我相当确信 Web API v8.2 不支持更改阶段。

最佳答案

我有一些代码试图将业务流程阶段向前移动,作为自定义工作流步骤(而不是插件)。我已经在下面发布了。

我看到的区别是:

  • 我在前进(而不是后退)
  • 我可能没有遵循最佳实践 :)
  • 我没有检索事件路径,我只是获取流程的所有可用阶段
  • 我还设置了 TraversedPath 属性

代码:

var activeInstancesRequest = new RetrieveProcessInstancesRequest
{
EntityId = TargetEntity.Id,
EntityLogicalName = TargetEntity.LogicalName
};
var activeInstancesResponse = (RetrieveProcessInstancesResponse)base.OrgService.Execute(activeInstancesRequest);
var process = activeInstancesResponse.Processes.Entities.Select(x => x.ToEntity<BusinessProcessFlowInstance>()).ToList();
var stages = base.XrmContext.ProcessStageSet
.Where(s => s.ProcessId.Id == process.FirstOrDefault().ProcessId.Id)
.Select(s => new ProcessStage
{
ProcessStageId = s.ProcessStageId,
StageName = s.StageName
})
.ToList();

var targetStage = stages.Where(stage => stage.StageName == targetStageName).FirstOrDefault();
if (targetStage != null)
{
crmWorkflowContext.Trace($"BPF contains target stage (\"{targetStageName}\"). Attempting to update BPF");

// Setting the Traversed Path is necessary for the Business Process Flow to show the active Stage
// If this is not updated then although the new Stage is set as current, the previous Stage remains actively selected
var traversedPath = $"{bpf.TraversedPath},{targetStage.ProcessStageId.Value}";
var update = new BusinessProcessFlowInstance()
{
BusinessProcessFlowInstanceId = bpf.BusinessProcessFlowInstanceId,
ProcessStageId = targetStage.ProcessStageId,
TraversedPath = traversedPath
};

xrmContext.Attach(update);
xrmContext.UpdateObject(update);
}

关于c# - 在 C# 插件中更改业务流程阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51430303/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com