March 19, 2007

Dates in CRM Queries

It's pretty simple to build query expressions from CRM web service. Until you get to dates. You have lots of different ConditionOperator types like On, OnOrAfter, OnOrBefore, ThisWeek, Last7Days etc. But what about the values?

The problem is that you can't add a DateTime object to the values array of the ConditionExpression. If you do you'll most likely get the super duper error "Server was unable to process request". And what is more! The exception data reveals us the intriguing reason: "Invalid argument." DOH!

You have to pass the DateTime as string and parse it with "s" parameter. This is language-neutral and the CRM Server should be able to parse it regardless of the regional settings.

Solution:

ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "new_duedate";
condition.Operator = ConditionOperator.OnOrAfter;
condition.Values = new string[] { DateTime.Now.ToString("s") };

 


AND YES! IT IS FINALLY SNOWING OUTSIDE!!!!!