Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Business Process Error System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Posted on by 195

Hi i just dive to this crm thing, i'm still very new to this so my friend recomend me to use plugin instead of javascript because he said that plugin is running in backend and javascript  code is running in frontend, so i tried some tutorials from ebook and i try to do this excersie , that have scenario like this 

The training solution you imported to Dynamics earlier contains an entity called ita_trainingconfiguration You need to make sure that, for any create or update of that entity, ita_notestallowedmessage attribute has some data (other than null). Can you add a new plugin to our current solution,

here is my code below

using Microsoft.Xrm.Sdk;
using System;


namespace TrainingConfiguration.Plugins
{
    public class ConfigurationValidation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            Entity entity = (Entity)context.InputParameters["Targets"];            
            string note = (string)entity["ita_notestallowedmessage "];
            if(note == null)
            {
                throw new InvalidPluginExecutionException("Field can't be empty");
            }
            
        }
    }
}

from what i catch from this tutorial is note is a field from the Entity which is a target depend what primary entity we choose in register new step, and then i make condition to check if this field is null or not if null it will throw the exception message. at first i tough it because i'm misspelled the name of the field but i'm already double check the field name and i'ts correct. Can someone give me some explanation here thanks

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Business Process Error System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

    Hi,

    While plugin executes , Dynamicis CRM passes input parameter depending on the plugin event,Target is one of the type of input parameter which you will get inside your plugin.It might be change so you have to make sure that Target is contais and its type is an entity.

    So once we confirms is Target is available and we are basically making sure that its a type of entity. After that we are getting the entity where the request has sent.

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

    You can check the entity name also after above line if your plugins will get fire from multiple Entity.

    For more information you can go through below reference -

    msdn.microsoft.com/.../gg309673.aspx

    You can do null check using below code -

                           if (entity.Attributes.Contains("ita_notestallowedmessage"))

                           {

                               if (entity.GetAttributeValue<string>("ita_notestallowedmessage") == null)

                               {

                                   throw new InvalidPluginExecutionException("Field can't be empty");

                               }

                           }

  • Keysersozeey Profile Picture
    Keysersozeey 195 on at
    RE: Business Process Error System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

    hi  Goutam Das thanks for your answer this answer is working, but can you explain me what this code do context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity , so in here we check if inputparameter containts  target which is  ita_trainingconfiguration and check if  ita_trainingconfiguration is entity or not  is my explanation true? and then in here

    string note = entity.Contains("ita_notestallowedmessage") ? entity["ita_notestallowedmessage"].ToString() : string.Empty;  and in this code we get note from fields ita_notestallowedmessage and we make null awareness and if it's null it will be string.Empty. please correct me if i'm wrong thanks in advance

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Business Process Error System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

    Hi,

    Try with this  -

    using Microsoft.Xrm.Sdk;
    using System;
    namespace TrainingConfiguration.Plugins
    {
        public class ConfigurationValidation : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    string note = entity.Contains("ita_notestallowedmessage") ? entity["ita_notestallowedmessage"].ToString() : string.Empty;
                    if (string.IsNullOrEmpty(note))
                    {
                        throw new InvalidPluginExecutionException("Field can't be empty");
                    }
                }
            }
        }
    }


  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Business Process Error System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

    Hi,

    Use Target instead of Targets and also check if the entity contains the required attribute. Please use the code below.

    using Microsoft.Xrm.Sdk;
    using System;
    namespace TrainingConfiguration.Plugins
    {
        public class ConfigurationValidation : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                Entity entity = (Entity)context.InputParameters["Target"];   
                if(entity.Attributes.Contains("ita_notestallowedmessage"))
                {         
                 string note = entity["ita_notestallowedmessage"].ToString();
                 if(note == null)
                 {
                    throw new InvalidPluginExecutionException("Field can't be empty");
                 }
                }            
            }
        }
    }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans