Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Monday, June 30, 2014

PlugIns - How to create the CRM Organization Service in a CRM 2013 C# Workflow

by Carmel Schvartzman
  1. This is the Building Block C# code  to create the CRM Organization Service in a CRM 2013 Workflow.
  2. The present code relates to a custom  Plugin / Workflow in Dynamics CRM 2011/2013. 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.
  3. Then you use this code to set up the Xrm Organization Service that you need to interact with the CRM organization:
protected override void Execute(CodeActivityContext context)
        {
            #region GET THE EXECUTION CONTEXT FROM THE SERVICE PROVIDER VARIABLE :
            execContext = context.GetExtension<IExecutionContext>();
            workflowContext =
                context.GetExtension<IWorkflowContext>();
            serviceFactory =
                context.GetExtension<IOrganizationServiceFactory>();
            service =
                serviceFactory.CreateOrganizationService(workflowContext.UserId);
                     
            ITracingService tracingService = context.GetExtension<ITracingService>();
           #endregion
            ///////////////////////////////////////////////////////////////////////////////////////////
            // Business Logic:
            ExecuteYourBussinessLogicMethod(context, workflowContext, tracingService);
            //////////////////////////////////////////////////////////////////////////////////////////
        }

                                                  That's all...Enjoy Dynamics CRM 2013


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

                                                  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


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