Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Wednesday, December 3, 2014

How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011)

  1. In this article we see how to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011) .

    How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011)

How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011)




  1. This error appears in Dynamics CRM 2011 - 2013 custom Plugins or custom Workflows, or any C# code calling the Organization Web Service WCF endpoint using late binding. Specifically we're creating a new address for an Account or a Contact entity. The CustomerAddress CRM entity has a reference field which points to the "ParentId" , the Account or Contact to which belongs this new address :

    How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011) 1
  2. The C# code above instantiates the "ParentId" field with the actual Account ID (guid) . The guid is guaranteed to contain a Value, elsewhere the code inside the brackets wouldn't be executed :
    How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011) 2
  3. However, there is an exception raised , expressing that the value is null : the error message is misleading, since the data type of the CRM SQL column is indeed "uniqueidentifier" , therefore a Guid, which we tried to insert in the attribute:

  4. How to fix the "Attribute: parentid cannot be set to NULL" exception in Dynamics CRM 2013 (2011) 3

  5. You wouldn't imagine that that is not enough for CRM, and that it is expecting to get another Type at all: an "EntityReference" CRM type, and not just a Guid:

     
     /*  CREATE PARENT REFERENCE FOR CUSTOMER ADDRESS  */
                    EntityReference parentID = new EntityReference();                    parentID.Id = guidParentAccount.Value;                    parentID.LogicalName = "account";
                    oAccountAddress.Attributes["parentid"] = parentID;
                        /***************************************************/

                      This way you get rid of the error, giving to the CRM Organization Service exactly what it needed.
                      There is another way to write the code above, shorter, as follows:

                            /*  CREATE PARENT REFERENCE FOR CUSTOMER ADDRESS  */
                                oAccountAddress["parentid"] = new EntityReference("account", guidParentAccount.Value );                                            /***************************************************/


                                    That's all...Enjoy Dynamics CRM

                                    by Carmel Schvartzman

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







                                    Tuesday, November 18, 2014

                                    How to install Business Intelligence Development Studio (BIDS) for Visual Studio 2012 Sql Server 2008


                                    1. In this walkthrough we will learn How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 
                                    2. The Business Intelligence Development Studio (BIDS) is an add-on to Visual Studio 2012 and Sql Server 2008. The BIDS includes the SSIS - Sql Server Integration Services templates for developing SSIS projects on Visual Studio 2012. Here we'll see step by step how to install it :

                                      SSIS - Sql Server Integration Services

                                    How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 Sql Server 2008






                                    1. Browse to the Microsoft Download Center to get the Visual Studio 2012 templates for the BIDS, which also includes the SSIS - Sql Server Integration Services :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008
                                    2. Download it to your machine and run it as administrator:

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 1
                                    3. Click the "Install" button :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 2
                                    4. Make sure that the Business Intelligence for Visual Studio 2012 box is checked  :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 3
                                    5. Click "Next" :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 4
                                    6. Check that the installation has been successful :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 5

                                       
                                    7. Now let's see how it has the Visual Studio 2012 upgraded. Before the installation, Visual Studio 2012 templates for new projects looked like this :

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 6
                                    8. Now, after the installation, the Visual Studio 2012 templates include the ones for Business Intelligence , SSIS between them:

                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 7
                                    9. Now you can create a new SSIS - Sql Server Integration Services project using the Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 :


                                      How to install Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 8

                                    10. In this walk-through we've learned how to install the Business Intelligence Development Studio (BIDS) on Visual Studio 2012 and Sql Server 2008 .
                                    11. That's all...Enjoy Dynamics CRM

                                      by Carmel Schvartzman

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


                                    Tuesday, November 11, 2014

                                    "Could not load file or assembly Microsoft.IdentityModel" exception in Dynamics CRM 2013 (2011)


                                    1. In this walkthrough we will learn how to take care of the "Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies" error in a Dynamics CRM 2013 (2011) related .Net application .

                                    The "Could not load file or assembly Microsoft.IdentityModel" exception


                                    1. Dynamics CRM 2011 - 2013 uses the Windows Identity Foundation in order to contact the Organization Web Service. 
                                    2. The "Could not load file or assembly 'Microsoft.IdentityModel" Exception's message tells us explicitly of the need of reference and loading an "assembly".
                                    3. However, the error message is misleading, since you wont have to reference certain assembly from your .Net project. Instead, you'll need to install a Windows's update : .
                                    4. First browse to the Microsoft Identity Foundation download web page  :

                                      "Could not load file or assembly Microsoft.IdentityModel" exception
                                    5. Check that your machine complains with the Microsoft Identity Foundation 's System Requirements.
                                    6. Check the "Install Instructions" to see which package is appropriate for your machine :
                                      "Could not load file or assembly Microsoft.IdentityModel" exception 1
                                      And download the one you need (the 64 bit package, for instance):

                                    7. "Could not load file or assembly Microsoft.IdentityModel" exception 2
                                    8. Locate the downloaded package and open it:

                                    9. Then follow the install steps until you finish the setup (notice that this is an update for Windows, the update KB974405):









                                    10. After you finish the installation, you will be tempted to follow the leads from the Exception message , "Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies" , by adding some assembly to your project:



                                      But, where is the assembly?
                                    11. Remember, this is an update for Windows , and not an assembly: the error message can mislead us ...
                                      You don't have to search some assembly, neither close-open your Visual Studio nor restart your machine. Actually, you don't have to do anything: just run again your .Net application, and you'll find that now is working perfectly.
                                    12. In this walk-through we've review how to fix the "Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies" error in a Dynamics CRM 2013 (2011) related .Net application .
                                    13. That's all...Enjoy Dynamics CRM

                                      by Carmel Schvartzman

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


                                    Sunday, August 31, 2014

                                    PlugIns - How to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow

                                    by Carmel Schvartzman
                                    1. This is the Building Block C# code to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow. When using the late binding approach , you would probably be faced with web services errors related to types mismatch . Usually , you would need to waste much time looking for the specific field which caused the error . The goal of this tutorial is to avoid falling into that kind of problems when developing CRM plugins with the late binding approach . 
                                    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. After you set up the Xrm Organization Service that you need to interact with the CRM organization, you can use the following code to COPY values fetched through the CRM Web Service, which we call here "sourceEntity",  to your local "targetEntity" . We include here 4 scenarios : an INT field, STRING, DECIMAL & DATETIME : each one of them will be copied SAFELY, meaning that because we are using late binding here, we will avoid causing web service exceptions :

                                    SAFELY COPY SOURCE VALUES TO TARGET FIELDS IN PLUGIN WORKFLOW

                                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
                                    ///////////////////////  CASE #1 ) STRING :  ////////////////////////////////////////
                                    if (sourceEntity.Attributes.ContainsKey("source_field"))               
                                         {                    
                                               targetEntity["target_field"] =  Convert.ToString(sourceEntity.Attributes["source_field"]);               
                                          }





                                    //////////////////////////////////////////////////////////////////////////////////////////////////
                                    /////////////////////////  CASE #2 ) INT :
                                    /////////////////////////////////

                                    if (sourceEntity.Attributes.ContainsKey("source_field") )            
                                    {                
                                       int iCode = 0;                
                                       if (Int32.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out iCode))                
                                       {                   
                                           targetEntity.Attributes["target_field"] = iCode;                
                                       }            
                                    }




                                    //////////////////////////////////////////////////////////////////////////////////////////////////             
                                     /////////////////  CASE # 3 ) MONEY - DECIMAL : //////////////////////
                                    if (sourceEntity.Attributes.ContainsKey("source_field") )            
                                    {                
                                       decimal mValue = 0.0M;               
                                        if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out mValue ))               
                                        {                    
                                             targetEntity.Attributes["target_field"] = mValue;                
                                         }            
                                    }




                                    //////////////////////////////////////////////////////////////////////////////////////////////////                      
                                    //////////  CASE #4 ) DATETIME : /////////////////////////////////
                                    if (sourceEntity.Attributes.ContainsKey("source_field") )           
                                    {               
                                             decimal mValue = 0.0M;               
                                             if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"]), out mValue ))               
                                           {                   
                                                targetEntity.Attributes["target_field"] = Convert.ToDateTime( sourceEntity.Attributes["source_field"]);               
                                            }           
                                    }


                                    ////////////////////////////////////////////////////////////////////////////////////////////////





                                    The present article is about how to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow.
                                    That's all...Enjoy Dynamics CRM 2013!!!


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


                                    Monday, July 28, 2014

                                    PlugIns - How to GET A FIELD VALUE FROM SOME CRM ENTITY - HAVING ITS GUID in a CRM 2013 C# Workflow

                                    by Carmel Schvartzman
                                    1. This is the Building Block C# code to GET A FIELD VALUE FROM SOME CRM ENTITY - HAVING ITS GUID in a CRM 2013 C# 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);
                                                  //////////////////////////////////////////////////////////////////////////////////////////
                                              }

                                    4. Then having already working the CRM Organization Service, and having also the GUID id of the required entity object, you can get the value for some selected field, as follows:


                                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   For this building Block  you MUST have the entity GUID    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                    ////  This is the Guid ID of the required record : Guid entityId = new Guid();
                                    ////  The fields you need to fetch : ColumnSet fields = new ColumnSet(                new string[]                 {                     "new_name", "new_custom_field"                 }                );

                                                Entity customEntity = service.Retrieve("new_custom_entity", entityId, fields);


                                    ////////  Get safely the source value and copy it to the target field in another entity: if (customEntity.Attributes.ContainsKey("new_custom_field"))                {                    targetEntity["target_field"] = Convert.ToString(customEntity.Attributes["new_custom_field"]);                }





                                    That's all...Enjoy Dynamics CRM 2013!!!


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

                                    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


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