【问题标题】:The given key was not present in the dictionary字典中不存在给定的键
【发布时间】:2012-02-18 03:52:03
【问题描述】:

我正在 crm 5.0 中开发一个插件,用于从名为“ct_marketvalue”的实体中检索日期“ct_valuedate”,并在名为“ct_dateserial”的字段中进行格式化和保存

调试“字典中不存在给定键”时出现错误

public class MarketValueDateFormatting : PluginBase
{
    protected override void ExecutePlugin()
    {

        try
        {

            switch (_crmMessage)
            {
                case CrmPluginMessageEnum.Create:


                    if (_context.InputParameters.Contains("ct_marketvalue"))
                    {
                        //Obtain the logical name of the entity
                        string moniker1 = ((EntityReference)_context.InputParameters["EntityMoniker"]).LogicalName;
                        //Verify that the target entity represents an Account.
                        //If not, this plug-in was not registered correctly.
                        if (moniker1.Equals("ct_marketvalue"))
                        {
                            Entity marketvalueimage = (Entity)_context.PostEntityImages["ct_marketvalue"];
                            Guid marketvalueid = marketvalueimage.Id;
                            if (marketvalueimage.Contains("ct_valuedate"))
                            {
                                DateTime dateserial = (DateTime)marketvalueimage.Attributes["ct_valuedate"];
                                String dateserialstring = dateserial.ToString("YYYYMMdd");


                                Ct_marketvalue marketvalue = new Ct_marketvalue();
                                marketvalue.Ct_dateserial = dateserialstring;
                                marketvalue.Id = marketvalueid;

                                _serviceContext.UpdateObject(marketvalue);

                            }
                        }
                    }

                    break;
                            }
        catch (Exception ex)
        {
            throw ex;
        }

    }

}

}

【问题讨论】:

    标签: plugins dynamics-crm crm


    【解决方案1】:

    关于您的代码的一些注释。

    1. 您应该在代码中检查 _context.PostEntityImages 是否包含“ct_marketvalue”。可能是忘记注册或者图片名称有误。

    2. 使用 .ToEntity 可能比使用 .Attributes["ct_valuedate"] 访问属性更好。

    3. 我不确定您编写的插件的用途是什么,但它看起来是后期插件,它更新了 InputParameters 中的同一个实体实例。直接在 InputParameters 中使这个插件预先阶段和更新值可能会更好。因为,如果不是“The given key was not present in the dictionary”异常,就会导致无限循环。您将需要检查 context.Depth。

    【讨论】:

    • @Paramosh-感谢您的意见。
    • 创建没有原像我必须使用后实体图像。我更改了代码。我仍然得到同样的错误。
    • 我注释掉了开关箱内的所有代码。我得到了同样的错误。
    猜你喜欢
    • 2016-08-14
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多