locked
Plugin "Update" does not run, "Create" runs just fine, Given Key Not Present in Dictionary RRS feed

  • Question

  • Does anyone know or have an idea why the code below: 

      DynamicEntity dynamicEntity = (DynamicEntity) context.InputParameters.Properties[ParameterName.Target];
                String entityName = dynamicEntity["name"].ToString();
                String entityId = context.OutputParameters.Properties["id"].ToString();

    runs fine for account creation but not for account update?  I get the "The given key was not present in the dictionary" error in the message, but I don't know why it would work for creation and not update....
    Thursday, April 8, 2010 9:32 PM

Answers

  • This line

     String entityName = dynamicEntity["name"].ToString();

    will only work if you modify this value on your form.

    You need to use

    if(dynamicEntity.Properties.Contains("name"))

    before reading the name property.

    But if you want the primary entity name of the plugin message then use context.PrimaryEntityName or if you want the DynamicEntity's from the InputParameters then use dynamicEntity.Name


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 8, 2010 9:46 PM
    Moderator

All replies

  • This line

     String entityName = dynamicEntity["name"].ToString();

    will only work if you modify this value on your form.

    You need to use

    if(dynamicEntity.Properties.Contains("name"))

    before reading the name property.

    But if you want the primary entity name of the plugin message then use context.PrimaryEntityName or if you want the DynamicEntity's from the InputParameters then use dynamicEntity.Name


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 8, 2010 9:46 PM
    Moderator
  • As Rhett states the entity contained in the "Target" will only contain those fields that have data submitted.  Even if you're certain the field will be submitted, it's important to check for the field's presence in the entity.  Rhett's code is a little incorrect when it comes to DynamicEntities.  The proper way to check for the presence of a field is:

    if (dynamicEntity.Properties.Contains("some_field"))

    I recommend checking for all fields you intend to manipulate/use in your code, as there are many ways that SOAP can be used to update a record not necessarily involving the entity's form (Imports come to mind).  You cannot always be certain that a field will be present in the "Target", even if you force its submission using the .ForceSubmit attribute of the field on the UI.


    Dave Berry
    Thursday, April 8, 2010 11:05 PM
    Moderator