Working with Date and Time Values in CRM 2011 Programmatically


Most of us working with CRM 2011 would have faced some issues working with CRM 2011 date time objects as they don’t represent the exact date time value that we see on CRM form.

This has been an issue when working with the programmatically as what we see as the date time value in the CRM form isn’t the value that is stored in the database for that field. The value we send gets converted and gets stored inside the date time field. When we check the record from CRM form, internally this value has got converted and we see the correct value. This is all good. But what happens when you want to get this field programmatically for one of our task. This is where we get the issue.

We came across an issue like this while working with one of our projects. We came up with the following solution to get around this issue.

·         We retrieve the user setting of the user who created the record.
·         Then we take the TimeZoneBias and TimeZoneDaylightBias fiels values from the user setting record and calculate the correct time from them.

UserSettings us = "Retreived user settings record";
DateTime dt = account.start_date.Value.AddMinutes(-Convert.ToDouble(us.TimeZoneBias));
dt = dt.AddMinutes(-Convert.ToDouble(us.TimeZoneDaylightBias));

The value is normally updated in the database in the CRM by adding users time zone minutes and deducting the day light saving value. We simply add them here to get the correct date and time. Simple ha!.....

Comments

Post a Comment

Popular posts from this blog

Error message “User does not have send-as privilege.” when sending emails programmatically in CRM 2011