Plugins 字典中不存在给定的键

Plugins 字典中不存在给定的键,plugins,dynamics-crm,crm,Plugins,Dynamics Crm,Crm,我正在crm 5.0中开发一个插件,从一个名为“ct\U marketvalue”的实体中检索日期“ct\U valuedate”,并格式化并保存在名为“ct\U dateserial”的字段中 调试“字典中不存在给定的密钥”时出错 }关于代码的几点注释 您应该在代码中检查context.PostEntityImages是否包含“ct\uMarketValue”。有可能忘记注册或在图像名称中出错 使用.ToEntity可能比使用.attributes[“ct\u valuedate”]访问属性更

我正在crm 5.0中开发一个插件,从一个名为“ct\U marketvalue”的实体中检索日期“ct\U valuedate”,并格式化并保存在名为“ct\U dateserial”的字段中

调试“字典中不存在给定的密钥”时出错


}

关于代码的几点注释

  • 您应该在代码中检查context.PostEntityImages是否包含“ct\uMarketValue”。有可能忘记注册或在图像名称中出错

  • 使用.ToEntity可能比使用.attributes[“ct\u valuedate”]访问属性更好

  • 我不确定你们写的插件的目的是什么,但它看起来是后期插件,它更新了相同的实体实例,那个是在InputParameters中。最好是使这个插件处于前置阶段,并直接在InputParameters中更新值。因为,如果不是“给定的键在字典中不存在”异常,它将导致无限循环。您需要检查context.Depth


  • @Paramosh感谢您的输入。Create没有前映像,我必须使用postentity映像。我更改了代码。我仍然收到相同的错误。我对交换机外壳内的所有代码进行了注释。我收到了相同的错误。
    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;
            }
    
        }
    
    }