expression conditions in MS CRM API

MS CRM API is a very nice thing, unfortunately for getting structured data from and to the CRM database. Unfortunately there is no possibility just to write a query in SQL  and get the results from it. Instead of this you have to create the whole object structure to represent the SQL.

For example, lets take a query, which returns all the orders from a selected reseller, which expire in the next 4 months:

select * from salesorder where new_resellerid= resellerX and new_expireddate <= today()+157

Easy enough? Now, let’s how it looks in C# using the API:”

queryExpression query = new QueryExpression();
query.EntityName = “salesorder”;
query.ColumnSet = new AllColumns();
FilterExpression filter2 = new FilterExpression();
ConditionExpression condition2_1 = new ConditionExpression();
condition2_1.AttributeName = “new_expireddate”;
condition2_1.Operator = ConditionOperator.NextXDays;
condition2_1.Values = new object[] { 157 };
ConditionExpression condition2_2 = new ConditionExpression();
condition2_2.AttributeName = “new_resellerid”;
condition2_2.Values = new string[] { Resellers[i].ToString() };
filter2.Conditions = new ConditionExpression[] { condition2_1, condition2_2 };
query.Criteria = filter2;
BusinessEntityCollection OrdersCollection = service.RetrieveMultiple(query2);

And this was an easy one, from just one table! I don’t actually know how to create complex queries from different tables.

You can leave a response, or trackback from your own site.

Sorry, no posts matched your criteria.