【问题标题】:CRM 2011 KeyNotFoundException exceptionCRM 2011 KeyNotFoundException 异常
【发布时间】:2012-03-28 05:15:37
【问题描述】:

我是 CRM 开发的新手。 我有一个自定义实体“客户”。该实体有一个名为“defaultcustomer”的字段,可以是 TRUE 或 FALSE。我正在开发一个插件,我需要将所有“客户”的“defaultcustomer”设置为 FALSE。我这样做如下:

事实:

我已经为实体“客户”本身注册了插件。因此,当实体“客户”更新时,插件会触发。

private void MakeAllNonDefault()
{

    try
    {
        QueryExpression query = new QueryExpression("customer");
        query.ColumnSet = new ColumnSet("defaultcustomer");

        EntityCollection retrieved = service.RetrieveMultiple(query);

        foreach (Entity myCustomer in retrieved.Entities)
        {

            myCustomer["defaultcustomer"] = false;
            service.Update(myCustomer);
        }

    }
    catch (Exception ex)
    {
        throw new InvalidPluginExecutionException("An error occurred in MakeAllNonDefault(): " + ex.ToString());
    }
}

错误: 它在这一行抛出错误:

myCustomer["defaultcustomer"] = false;

System.Collections.Generic.KeyNotFoundException: 
The given key was not present in the dictionary. 

【问题讨论】:

  • 如果您使用早期绑定并将返回的实体强制转换为您的 Customer 实体的实例,这种类型的错误就会消失。

标签: dynamics-crm-2011 keynotfoundexception query-expressions


【解决方案1】:

该错误表示属性集合中不存在特定字段。在 CRM 中,仅包含已设置或更新的属性。

尝试类似:

foreach (Entity myCustomer in retrieved.Entities)
{
    if (myCustomer.Attributes.ContainsKey("defaultcustomer"))
    {
        myCustomer["defaultcustomer"] = false;
    }
    else
    {
        myCustomer.Attributes.Add("defaultcustomer", false);
    }
    service.Update(myCustomer);
}

【讨论】:

  • 这是可能的,即使他在新 QueryExpressionColumnSet 中明确包含该属性?
  • @PeterMajeed - 据我所知,如果字段中没有值,则不会返回。
  • @glosrob:也许这是不使用后期绑定的另一个原因。我们的小组根本不使用后期绑定,但我希望如果您明确包含该属性并且该属性的值为NULL,该属性将出现在属性集合中,其值为NULL,并且将是可用于赋值。
【解决方案2】:

您是否仔细检查过该字段是否真的被称为 defaultcustomer?

如果它是自定义实体,则该字段可能以前缀开头,例如 new_defaultcustomer。确保您使用的是字段名称,而不是显示名称。

【讨论】:

  • 其实这是一个很好的观点。如果是自定义实体,据我所知,所有字段(除了默认的 createdby、created 等)都会有前缀。
【解决方案3】:

@glosrob 发布的解决方案看起来不错。您是否仍然收到“给定的键不在字典中”?

尝试使用 ITracingService 获取有关插件执行流程的更多信息。

【讨论】:

    【解决方案4】:

    虽然更新所有 Crm 字段均为 False,但接受您更新该字段的内容。为此,您可以在插件中使用前置/后置图像。您将找到 crm 字段键并更新您需要的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多