CRM 2011 KeyNotFoundException异常 [英] CRM 2011 KeyNotFoundException exception

查看:94
本文介绍了CRM 2011 KeyNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CRM开发的新手。
我有一个自定义实体客户。该实体具有一个名为 defaultcustomer的字段,该字段可以为TRUE或FALSE。我正在开发一个插件,其中需要为所有客户将 defaultcustomer设置为FALSE。我正在执行以下操作:

I am new to CRM development. I have a Custom Entity "customer". This Entity has a Field called "defaultcustomer", which can be TRUE or FALSE. I am working on a Plug-In where i need to set the "defaultcustomer" to FALSE for all the "Customers". I am doing it as below:

事实:

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

I have registered the plugin for the entity "customer" itself. So when the Entity "customer" is updated, the plugin fires.

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

错误:
在此行上引发错误:

ERROR: It throws error on this line:

myCustomer["defaultcustomer"] = false;

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


推荐答案

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

The error means that particular field is not present in the collection of properties. In CRM, only properties that have been set or updated are included.

尝试类似的操作:

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

这篇关于CRM 2011 KeyNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆