CRM 2011在线-插件中的无限循环错误-无法找到它

本文关键字:错误 在线 2011 插件 CRM 无限循环 | 更新日期: 2023-09-27 17:53:12

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

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

}

这个插件执行更新的机会,但它抛出以下错误:

此工作流作业因启动它的工作流而被取消包括一个无限循环。请更正工作流逻辑,然后再试一次。

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

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
谁能详细说明一下这个错误吗?

CRM 2011在线-插件中的无限循环错误-无法找到它

在某个地方,您有一个插件,它生成一个循环循环,您没有突破,请参阅我关于my_autonumber实体的问题。

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

:后

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

插入一行:

if (context.Depth > 1) return;

如果插件执行了不止一次,这将终止处理。

注意使用context.Depth。

使用上下文。Depth会使你的插件失败,因为你的插件中有另一个插件触发了这个动作。

现在深度将是2,但这是因为有另一个插件叫这个插件。

我也面临这个问题,除了使用Context.Depth还有其他解决方案吗?