locked
"The given key was not present in the dictionary"; RRS feed

  • 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

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)
    Мой блог (русскоязычный)
    Tuesday, September 14, 2010 10:42 AM
    Moderator
  • Also if you have not changed value for new_stage you should get this error
    Mahain : http://mahenderpal.wordpress.com
    Tuesday, September 14, 2010 10:45 AM
    Moderator
  • 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
    Tuesday, September 14, 2010 12:45 PM
    Moderator