365 CRM 字典中不存在给定的键
365 CRM The given key was not present in the dictionary
我正在尝试为 crm 编写一个插件,但它总是有异常
The given key was not present in the dictionary.
此错误总是出现在以下行
Entity entity = (Entity)context.InputParameters["Targets"];
我已经尝试在 Internet 上查找,但仍然无法理解为什么抛出此异常。我已经在我的插件上添加了登录密钥,
谁能解释这个异常以及如何解决这个问题?下面是我的代码:
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");
}
}
}
}
}
}
应该是Target
没有's':
Entity entity = (Entity)context.InputParameters["Target"];
我认为 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 中可用。导航到 设置 > 插件跟踪日志,应该有所有插件执行的历史记录。
我正在尝试为 crm 编写一个插件,但它总是有异常
The given key was not present in the dictionary.
此错误总是出现在以下行
Entity entity = (Entity)context.InputParameters["Targets"];
我已经尝试在 Internet 上查找,但仍然无法理解为什么抛出此异常。我已经在我的插件上添加了登录密钥,
谁能解释这个异常以及如何解决这个问题?下面是我的代码:
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");
}
}
}
}
}
}
应该是Target
没有's':
Entity entity = (Entity)context.InputParameters["Target"];
我认为 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 中可用。导航到 设置 > 插件跟踪日志,应该有所有插件执行的历史记录。