Friday, 12 August 2016

Check whether a date field has valid data in MS CRM

The date type unlike other object types is not equal to null when it is not populated in C#.
Say, you are writing a plugin (in Dynamics CRM) and you expect the date field to be filled and do some operation on it (like add some days) and then use this value as attribute value in some other entity during create.

The problem is that the minimum value of date field gets populated when there is no date value in the date variable. The default minimum date in .NET is 1/1/0001 while CRM accepts a minimum date equal to 1/1/1900. So when we try to create the new record with a non-existent date value from the other entity, an exception is thrown by the platform stating the above fact.

So the way to check whether the date field contains a valid value in your C# code is:

// value is not provided in the email
if (emailStartDate == DateTime.MinValue)
{
  // do something
}
else
{
 // valid value!!
}

Hope this helps!
Happy Dynamics CRMing...

No comments:

Post a Comment