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

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







                                    No comments:

                                    Post a Comment