Friday, 26 April 2013

The given key was not present in the dictionary - what it means, and how to avoid it

A common error posted on the CRM Development forum is ‘the given key was not present in the dictionary’. This is a relatively easy error to diagnose and fix, provided you know what it means. It will also help to identify the line in the code at which the error occurs, which is most easily determined by debugging.

The error refers to a ‘dictionary’, and a ‘key’. The ‘dictionary’ is a type of collection object (i.e. it can contain many values), and the ‘key’ is the means by which you specify which value you want. The following two lines of code both show an example:
Entity e = context.InputParameters["Target"];
string name = e.Attributes["name"];  // Note that this is equivalent to: string name = e["name"];

In the first line, InputParameters is the dictionary, and "Target" is the key. In the second line, Attributes is the dictionary, and "name" is the key. The error ‘The given key is not present in the dictionary’ simply means that the dictionary does not have a value that corresponds to the key. So, this error would occur in the first line if InputParameters does not contain a key called "Target", or in the second line if there were no "name" in Attributes.

The way to avoid these errors is simple; test if the key exists before trying to use it. Different collection classes can provide different ways to perform this test, but the collection classes in the CRM SDK assemblies all inherit from an abstract DataCollection class that exposes a Contains method, so you can use a consistent approach across these collection classes.
if (context.InputParameters.Contains("Target"))
 Entity e = context.InputParameters["Target"];
 if (e.Attributes.Contains("name"))
  string name = e.Attributes["name"];

There are a few common reasons of the use of CRM collection classes where a key might not be present when you expect it:
  • Within a plugin, the values in context.InputParameters and context.OutputParameters depend on the message and the stage that you register the plugin on. For example, "Target" is present in InputParameters for the Create and Update messages, but not the SetState message. Also, OutputParameters only exist in a Post stage, and not in a Pre stage. There is no single source of documentation that provides the complete set of InputParameters and OutputParameters by message and stage, though this post provides a list of the most common ones for CRM 4, and most of these still apply in CRM 2011
  • The Attributes collection of an Entity will only contain values for attributes that have a value. You may get the Entity from a Retrieve or RetrieveMultiple having specified a ColumnSet with the attribute you want, but this attribute will not be present in the Attributes collection if there were no data in that attribute for that record
  • Within a plugin, the Attributes collection of an Entity that you obtain from the "Target" InputParameter will only contain attributes that were modified in the corresponding Create or Update method. Using the example above, if this were in a plugin registered on the Update message, the "name" attribute would only be present if the "name" attribute was changed as part of the Update; the "Target" InputParameter will not contain all the attributes for the entity


Unknown said...
This comment has been removed by the author.
Unknown said...

Thank you very much..

Anonymous said...

This is fantastic. I could not figure out why I was getting this error until I read the section on updates only grabbing the keys that were updated. Thank you.

Muddassir said...

Thanks for sharing a best informetion
CRM Development Dubai:ERP Development Dubai

ERP said...

We are ERPTREE Leading oracle fusion HCM Online Training institute. We are providing online training services since 1999. still we are adding more oracle related courses as the technology changes. 2000+ online courses are available. we all ways achieve our goal in satisfying students in result we have global recognition to our site. we have best faculty for all of our online courses.

Oracle Fusion HCM Training

Unknown said...

Excellent site you’ve got here.. It’s difficult to find excellent writing like yours nowadays. I honestly appreciate people like you! Take care!!

user3srsoftwaresolurtions said...

Greed is a common enemy which
conquers so many of us.

So when you see an opportunity
to create wealth it’s natural
that greed can influence our

However, greed can lead to
indiscipline and sabotage.

The proof of wealth you
will see on this private page
will get your eyes rolling:

However the key to using
the information in this
video to your benefit, is
to stay focused and stay

Usually that’s easier said
than done.

However when you have a team
as successful as this to
guide you step-by-step
there is very little opportunity
to fail.

Go to this private page right now.

It could be the turning point
in your quest for financial

gotenham said...

Thank you! this error was giving me trouble but your explanation made me realise that the field u was pulling from had a NULL value, which was obviously not present in the lookup field...THANKS!

Best Advertising Agency Software said...

Thank You for submitting this Blog.Blog Information is very Useful for us.

Best Advertising Agency Software said...

Congrats! Nice blog. Thank You for submitting this Blog.Blog Information is very Useful for us.

Home Workout said...

Thank You For Submitting this Blog.

Earn By Learn said...
This comment has been removed by the author.
Earn By Learn said...

Nice Blog , Blog Information is very helpful for us. Thank You For Submitting this Blog.

Best Advertising Agency Software said...

Great Post and Need Complete Solution for Advertising Management Software