Monday, 28 July 2008

Clearing a value from a DynamicEntity object

Hi all,

This was by no means something I discovered myself, Patrick Verbeeten suggested this as a solution to a problem I had in the forums. I thought it would be a good idea to pass on the information though.

Lets say you have a DynamicEntity object and you need to clear one of the values in a plugin, for example the parentid of a contact. Normally, to add an attribute to a DynamicEntity you would use the entity.Properties.Add(...) method, but the problem is that CRM only processes values that has a value.

What I tried initially was to remove the value using the entity.Properties.Remove(attributename) method. This did delete the attribute, but on saving the DynamicEntity, it did not save this attribute to the server but instead left it the way it was.

What Patrick suggested was to use the null value coupled with each crm attribute type, example CrmDateTime.Null or CrmNumber.Null. So what I did was to add the null value to the PropertyBag and set the acctual value to null.

entity.Properties.Add(new CrmDateTimeProperty(CrmDateTime.Null));

What is very important here is that each attribute type has it's own Null value, you cannot interchange CrmDateTime.Null and CrmNumber.Null.

The only exception to this seems to be a string type attribute, you only need to assign the value as an empty string "". Note that you may also be able to use the String.Empty method, I have not tried this yes but would say that this should also work.


Paul Reyneke