web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

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


  • Keysersozeey Profile Picture
    195 on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

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

                               }

                           }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans