Building a QueryExpression where name field is either A or B

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-06-16T06:03:36Z Indexed on 2010/06/16 6:12 UTC
Read the original article Hit count: 271

Filed under:
|
|

I'm trying to build a Dynamics CRM 4 query so that I can get calendar events that are named either "Event A" or "Event B".

A QueryByAttribute doesn't seem to do the job as I cannot specify a condition where the field called "event_name" = "Event A" of "event_name" = "Event B".

When using the QueryExpression, I've found the FilterExpression applies to the Referencing Entity. I don't know if the FilterExpression can be used on the Referenced Entity at all. The example below is something like what I want to achieve, though this would return an empty result set as it will go looking in the entity called "my_event_response" for a "name" attribute. It's starting to look like I will need to run several queries to get this but this is less efficient than if I can submit it all at once.

ColumnSet columns = new ColumnSet();
columns.Attributes = new string[]{ "event_name", "eventid", "startdate", "city" };

ConditionExpression eventname1 = new ConditionExpression();
eventname1.AttributeName = "event_name";
eventname1.Operator = ConditionOperator.Equal;
eventname1.Values = new string[] { "Event A" };

ConditionExpression eventname2 = new ConditionExpression();
eventname2.AttributeName = "event_name";
eventname2.Operator = ConditionOperator.Equal;
eventname2.Values = new string[] { "Event B" };

FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.Or;
filter.Conditions = new ConditionExpression[] { eventname1, eventname2 };

LinkEntity link = new LinkEntity();

link.LinkCriteria = filter;

link.LinkFromEntityName = "my_event";
link.LinkFromAttributeName = "eventid";

link.LinkToEntityName = "my_event_response";
link.LinkToAttributeName = "eventid";

QueryExpression query = new QueryExpression();
query.ColumnSet = columns;
query.EntityName = EntityName.mbs_event.ToString();
query.LinkEntities = new LinkEntity[] { link };

RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;

return (RetrieveMultipleResponse)crmService.Execute(request);

I'd appreciate some advice on how to get the data I need.

© Stack Overflow or respective owner

Related posts about dynamics-crm

Related posts about mscrm