Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Wednesday, April 30, 2014

"The given key was not present in the dictionary" KeyNotFoundException Exception in CRM 2013 Plugin or custom Workflow

by Carmel Shvartzman

In this walkthrough we will learn how to take care of the "The given key was not present in the dictionary" - "KeyNotFoundException" error in a Dynamics CRM 2013 (2011) Plug-in or custom Workflow.
  1. A disadvantage of using late-binding when coding a CRM 2013 Plugin is, you shoud be very careful with the attributes names, since there is no compile time checking. 
  2. Specially when you have added a new field to an entity, and you get the "The given key was not present in the dictionary" Exception, you'll be prone to think something is not good with the new field definitions, or worse , the Organization Web Service is not recognizing the new field, or even worse, the Web Service needs to be reset to render the field, or may be you'd be desperately thinking to restart the CRM server to refresh the Web Service.
  3. That's because the error message by CRM may be misleading : you may be expecting that, in case your new field contains a null value, CRM Retrieve() or RetrieveMultiple() or Execute() will locate the field attribute, and fetch a NULL value for it. But CRM wouldn't : it WILL NOT INCLUDE THE KEY IN THE ATTRIBUTE'S DICTIONARY UNLESS IT CONTAINS A VALUE : if your field is not a REQUIRED field, you must code a checking for the presence or absence of the field.
  4. For instance, let's say you've added a new field named "new_test", and inside your Plug In you fetch it as follows:
  5. If the present entity has a NULL value in it , you'll be faced to the "The given Key..." exception:
  6. First think you'll want to check, is whether you requested for the field in the ColumnsSet:
  7. But that's not the problem. You did indeed. However, the "new_test" field attribute IS NOT THERE:

  8. That's because CRM WILL NOT INCLUDE THE KEY IN THE ATTRIBUTE'S DICTIONARY UNLESS IT CONTAINS A VALUE (meaning it was SET or UPDATED).
  9. You MUST perform a check for the field existence, but not a NULL check as this:
  10. You should instead use the ContainsKey(string) method from the Collection, for EVERY NO REQUIRED FIELD on the Entity:



    Hoping it helped you.
    That's all...Enjoy Dynamics CRM


    כתב: כרמל שוורצמן

Tuesday, December 24, 2013

How to debug a CRM 2011 Custom Plugin / Workflow

by Carmel Schvartzman

  1. In this walkthrough we will learn Step - By - Step How to debug a custom  Plugin / Workflow in Dynamics CRM 2011. Once you have developed your custom workflow, you deploy the assembly containing the plugin in the CRM web server, and register it with the Plugin Registration Tool of the CRM 2011 SDK.
  2. The problem is, every time you change the code, and recompile the assembly, you must redeploy it and the process of debugging-compiling-deploying-registering is very time-consumer and this cycle restarts for every bug and every update you may want to do.
  3. The best and quickest way of debugging-redeploying  your plugin is inside the CRM server itself. If you upgrade your code from inside the CRM web server, the process of debug and redeploy it will be far more quicker. You can also perform the testings against a test Organization.
  4. First, check whether or not Visual Studio 2010-2012 is installed in the CRM web server. If it doesn't, install it.
  5. Next, copy the workflow solution to the CRM server, or else develop entirely your  Plugin from the Visual Studio in the CRM server.
  6. Provided you have already built your workflow project, you must deploy it in the CRM server. You copy the assembly from the BIN folder of your project, to the special folder where CRM expects the plugins to be ( Microsoft Dynamics Crm\Server\Bin\Assemply ). Then, open the Plugin Registration Tool, and select "Register New Assembly":
  7. How to debug a CRM 2011 Custom Plugin / Workflow
  8. Specify the assembly location:
  9. Select the "Database" option:
  10. Now that your plugin has been registered, you can debug it this way: switch to the workflow project opened in the Visual Studio installed on the CRM web server:
  11. On the "Debug" tab, select "Attach to Process":
  12. Now select the two "CrmAsynchService.exe" which are running on the CRM server. Those are the services which take care of the plugins, workflows, and every asynchronic processes being run on the CRM 2011:
  13. Press the "Attach" for each one of the processes:
  14. Now debug your workflow at your ease. Open your CRM account and call the workflow which calls your custom plugin/workflow. Wait some seconds (it's an asynchronic process so CRM will decide by its own when to run your plugin) and you'll see that the breakpoints you have set on your code will be reached.
  15. Let's say that you've found a bug or you need to upgrade the code. You compile it and next you must redeploy the assembly and update the registration of the plugin, and debug again.  Therefore, open the BIN folder of your workflow project, and copy the .dll:
  16. Now open the folder where are stored the CRM custom plugins ( Microsoft Dynamics Crm\Server\Bin\Assemply ):
  17. Paste there the assembly. Next, open the Plugin Registration Tool, select your assembly and press "Update":
  18. Specify the location of the assembly to update:
  19. Finally click the button "Update Selected Plugins":



    And now you can debug again your plugin and see how the breakpoints are reached by the runtime.


    That's all...Enjoy Dynamics CRM


    כתב: כרמל שוורצמן