Search code examples
dynamics-crmcrmdynamics-crm-4dynamics-crm-2013

CRM 2013 Entity.GetAttributeValue<T> datetime to string


When adding an date record to crm 2013, its removing 2 hours from my record. Which is an expected behavoir of the crm as a timezones correction.

The next expected behavior, retreiving the entity using the organisationService RetrieveMultiple api call, returns me the same record without the extra 2 hours.

Now the strange magic begins, when using the Entity.GetAttributeValue(fieldname) it should give me the datetime with the extra 2 hours as it is a setting from the crm. But this is what i get:

Create record: '01/10/2014'
CRM stores: '30/09/2014 22:00:00'
Retreive entity results to: '30/09/2014 22:00:00'
Use Entity.GetAttributeValue<DateTime>(fieldname) results to: '30/09/2014 00:00:00'

Why does it not say 01/10/2014 after using that method ?


Solution

  • GetAttributeValue is just an helper, if the DateTime field value inside CRM is null, GetAttributeValue returns DateTime.MinValue.

    You need always to use .ToLocalTime() to get the local DateTime value instead of the UTC value.