Search code examples
c#dynamics-crmdynamics-crm-365

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,

enter image description here

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

                }




        }
    }
}

Solution

  • It should be Target without 's':

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

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