gpt4 book ai didi

c# - CRM 2011 Online - 插件中的无限循环错误 - 无法找到它

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:40 25 4
gpt4 key购买 nike

我正在使用以下在机会更新时执行的插件:

public class PreOpportunityWin : Plugin
{
public PreOpportunityWin() : base(typeof(PreOpportunityWin))
{
base.RegisteredEvents.Add(
new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "opportunity", new Action<LocalPluginContext>(ExecuteAutonumber)));
}

protected void ExecuteAutonumber(LocalPluginContext localContext)
{
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)localContext.PluginExecutionContext;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//Organization Service
IOrganizationService service = localContext.OrganizationService;
//Tracing Service
ITracingService trace = (ITracingService)localContext.TracingService;

Entity Target = (Entity)context.InputParameters["Target"];
var entity = service.Retrieve(
Target.LogicalName, Target.Id, new ColumnSet(true));
var entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)
{
//Code to execute if opportunity won
trace.Trace("In the execute block...");

//Depending on the retrieved name, generate the appropriate fetch xml
string fetchXml = null;
fetchXml = @"<fetch mapping='logical'>
<entity name='my_autonumber'><all-attributes/>
<filter type=""and"">
<condition attribute=""my_autonumberentity"" operator=""eq"" value=""opportunity"" />
<condition attribute=""my_name"" operator=""eq"" value=""The Autonumber Record"" />
</filter></entity></fetch>";

try
{
//Fetch the approiate autonumber record
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
string nextIncrementNumber = string.Empty;
if (result.Entities.Count == 1)
{
Entity autoNumber = result.Entities[0];

//Lock the autonumber enity
lock (autoNumber)
{
if (!autoNumber.Attributes.Contains("my_counter"))
throw new InvalidPluginExecutionException("my_counter must contain a value");
if (!autoNumber.Attributes.Contains("my_incrementunit"))
throw new InvalidPluginExecutionException("my_IncrementUnit must contain a value");
int counter = Int32.Parse(autoNumber.Attributes["my_counter"].ToString());
int incrementUnit = Int32.Parse(autoNumber.Attributes["my_incrementunit"].ToString());
string prefix = autoNumber.Attributes.Contains("my_prefix") ? autoNumber.Attributes["my_prefix"].ToString() : string.Empty;
string prefixSeparator = autoNumber.Attributes.Contains("my_prefixseparator") ? autoNumber.Attributes["my_prefixseparator"].ToString() : string.Empty;
string suffix = autoNumber.Attributes.Contains("my_suffix") ? autoNumber.Attributes["my_suffix"].ToString() : string.Empty;
string suffixseparator = autoNumber.Attributes.Contains("my_suffixseparator") ? autoNumber.Attributes["my_suffixseparator"].ToString() : string.Empty;
string numberFormatter = autoNumber.Attributes.Contains("my_numberformatter") ? autoNumber.Attributes["my_numberformatter"].ToString() : string.Empty;

string fieldToUpdate;
if (autoNumber.Attributes.Contains("my_entityautonumberfield"))
fieldToUpdate = autoNumber.Attributes["my_entityautonumberfield"].ToString();
else
throw new InvalidPluginExecutionException("my_entityautonumberfield should not be empty");
nextIncrementNumber = BuildAutoNumber(entity, prefix, prefixSeparator, suffix, suffixseparator, counter, incrementUnit, numberFormatter, service);

//Set project number
entity.Attributes["new_projectnumber"] = nextIncrementNumber;
autoNumber.Attributes["my_counter"] = counter + incrementUnit;
service.Update(autoNumber);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(string.Format("An error occured in Autonumber plugin: {0}", ex.ToString()));
}
}
}
}

//This function builds the autonumber itself
private string BuildAutoNumber(Entity entity, string prefix, string prefixSeparator, string suffix, string suffixSeparator, int counter, int incrementUnit, string numberFormatter, IOrganizationService service)
{
bool hasPrefix = false, hasSuffix = false;
string returnNumber = string.Empty;

prefix = "P";
if (!string.IsNullOrEmpty(prefix))
{
hasPrefix = true;
}
counter = counter + incrementUnit;
returnNumber = (hasPrefix ? prefix + prefixSeparator : "") + counter.ToString(numberFormatter) + (hasSuffix ? suffix + suffixSeparator : "");
return returnNumber;
}

此插件在机会更新时执行,但会抛出以下错误:

This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again.

我在任何地方都找不到无限循环,此外,我在创建机会时使用几乎相同的代码在不同的字段中附加另一个自动编号。两个插件之间的唯一区别是这段检查获胜状态的代码:

Entity Target = (Entity)context.InputParameters["Target"];
var entity = service.Retrieve(Target.LogicalName, Target.Id, new ColumnSet(true));
var entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)
//Code to execute if opportunity won

有人可以为我详细说明这个错误吗?

最佳答案

在某个地方,您有一个插件可以生成您不会中断的重复循环,请参阅我关于 my_autonumber 实体的问题。

要检查的值是 IPluginExecutionContext.Depth,它告诉您循环了多少次。

之后:

Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)localContext.PluginExecutionContext;

插入这一行:

if (context.Depth > 1) 返回;

如果插件执行多次,这将终止处理。

关于c# - CRM 2011 Online - 插件中的无限循环错误 - 无法找到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303823/

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