365 CRM字典中不存在给定的密钥

时间:2018-07-25 08:12:47

标签: c# dynamics-crm dynamics-crm-365

我正在尝试为crm编写一个插件,但是它总是有异常

  

字典中不存在给定的键。

此错误始终出现在下一行

  

Entity entity = (Entity)context.InputParameters["Targets"];

我已经尝试在Internet上查找,但仍然不明白为什么抛出此异常。我已经在插件上添加了登录密钥,

enter image description here

任何人都可以解释此异常以及如何解决此异常吗?这是我的代码如下:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;

namespace TrainingConfiguration.Plugins
{
    public class CreditValidation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


                Entity entity = (Entity)context.InputParameters["Targets"];
                EntityReference configurationEntitiy = (EntityReference)(entity.Attributes["ita_configuration"]);
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                if (entity.LogicalName.Equals("ita_creditlimit"))
                {
                    string configurationId = configurationEntitiy.Id.ToString();
                    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                      <entity name='ita_creditlimit'>
                      <attribute name='ita_creditlimitid' />
                      <attribute name='ita_name' />
                      <attribute name='ita_creditrating' />
                      <attribute name='ita_configuration' />
                      <order attribute='ita_name' descending='false' />
                      <filter type='and'>                     
                      <condition attribute='ita_configuration' operator='eq' value=""{0}"" />
                      </filter>
                      </entity>
                      </fetch>";
                    fetchXml = string.Format(fetchXml, entity.Id);
                    var qe = new FetchExpression(fetchXml);
                    var result = service.RetrieveMultiple(qe);
                    var rating = (OptionSetValue)entity["ita_creditrating"];
                    int selectedrating = rating.Value;
                    string ratingvalue = entity.FormattedValues["ita_creditrating"].ToString();
                    if (result.Entities.Count < 0)
                    {
                        List<String> listCreditRating = new List<string>();
                        for (int i = 0; i < result.Entities.Count; i++)
                        {
                            string creditRating = (string)result.Entities[i].Attributes["ita_creditrating"];

                            listCreditRating.Add(creditRating);
                        }
                        bool alreadyExist = listCreditRating.Contains(ratingvalue);
                        if(alreadyExist == true)
                        {
                            throw new InvalidPluginExecutionException("Data Already Exist");
                        }
                    }

                }




        }
    }
}

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

我认为JCJR是正确的,但我想扩大我的评论。

您可以从serviceProvider获取插件跟踪服务的实例:

var configurationEntitiy = (EntityReference)(entity.Attributes["ita_configuration"]);
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);

// New line to retrieve Trace
var traceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

然后您可以像这样使用跟踪服务:

traceService.Trace("Retrieving Entity from \"Target\" input parameter");
var entity = (Entity)context.InputParameters["Target"];

这些消息在CRM中可用。导航到设置> 插件跟踪日志,并且应该有所有插件执行的历史记录。