简体   繁体   中英

365 CRM The given key was not present in the dictionary

I'm trying to write a plugin for crm but it always has an exception

The given key was not present in the dictionary.

This error always occurs on the following line

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

I already try find on the internet but still can't understand why this exception was thrown. I already add signin key on my plugin either,

在此处输入图片说明

Can anyone explain this exception and how to fix this? Here is my code below :

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

                }




        }
    }
}

It should be Target without 's':

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

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/understand-data-context-passed-plugin#input-and-output-parameters

I think JCJR is correct, but I wanted to expand on my comment.

You can get an instance of the Plugin Trace Service from 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));

Then you can use the tracing service like so:

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

These messages are available in CRM. Navigate to Settings > Plug-in Trace Log and there should be a history of all your plugin executions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM