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.


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




Richard Bos said...

Thanks! You saved me loads of time :D

hardvin cadavid said...

Sorry my Friend. i Try your solution But Doesn't work, The VS Throw me this exception:

contains data from a type that maps to the name 'System:String[]'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'String[]' and namespace 'System