Answered by:
"The given key was not present in the dictionary";

Question
-
Hi ,
First time when I save a record system is throwing the below mentioned error but if I try to save again, system is saving without any error.
ERROR: "The given key was not present in the dictionary";
Here is the code.. please let me know what should i change in my code..
public void CheckAccessLevel(IPluginExecutionContext context)
{
ICrmService service = context.CreateCrmService(true);
Guid initiatingUserId = context.InitiatingUserId;
DynamicEntity entity = (DynamicEntity)context.InputParameters[ParameterName.Target];
Picklist StageCodePicklist = (Picklist)entity.Properties["new_stage"]; -> here I am getting error.
Thanks in advance.
Regards//- Bhaskar.
Tuesday, September 14, 2010 10:33 AM
Answers
-
Hi.
You should use following code:
Picklist StageCodePicklist = null;
if (entity.Properties.Contains("new_stage"))
StageCodePicklist = (Picklist)entity.Properties["new_stage"];
But in the case new_stage field is not inside of your entity - StageCodePicklist variable will be null.
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)- Proposed as answer by Andrii ButenkoMVP, Moderator Tuesday, September 14, 2010 10:42 AM
- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 10:42 AMModerator -
Bhaskar,
Before assignment check for
if (entity.Properties.Contains("new_stage")) as Andriy mentioned, then you should not get any error
Mahain : http://mahenderpal.wordpress.com- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 12:45 PMModerator -
Also if you have not changed value for new_stage you should get this error
Mahain : http://mahenderpal.wordpress.com- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 10:45 AMModerator
All replies
-
Hi.
You should use following code:
Picklist StageCodePicklist = null;
if (entity.Properties.Contains("new_stage"))
StageCodePicklist = (Picklist)entity.Properties["new_stage"];
But in the case new_stage field is not inside of your entity - StageCodePicklist variable will be null.
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)- Proposed as answer by Andrii ButenkoMVP, Moderator Tuesday, September 14, 2010 10:42 AM
- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 10:42 AMModerator -
Also if you have not changed value for new_stage you should get this error
Mahain : http://mahenderpal.wordpress.com- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 10:45 AMModerator -
Thank you. So can you please let me know, how will I handle null value of the picklist as new_stage field is not inside of my entity .
Bhaskar
Tuesday, September 14, 2010 12:39 PM -
Bhaskar,
Before assignment check for
if (entity.Properties.Contains("new_stage")) as Andriy mentioned, then you should not get any error
Mahain : http://mahenderpal.wordpress.com- Marked as answer by Donna EdwardsMVP Thursday, September 16, 2010 1:54 PM
Tuesday, September 14, 2010 12:45 PMModerator