locked
Building Dynamic QueryExpression in a loop RRS feed

  • Question

  • I've been extending territories in MS CRM 4 and have hit a slight snag, I created a custom entity,'Region', and made a 1:N from Territory to Region. The goal is that Region will contain rules that determine which accounts get assigned to which territory. I've written a pre-plugin on Account creation that queries regions and finds the right territory and that works quite well. The issue is the reverse direction, whenever someone changes the regions under a territory, I'd like users to be able to run a workflow against that territory record to reflect changes out on to all accounts (updating their territories based on the region rules).

    I need to have a dynamic query in my custom workflow, that is building a QueryExpression in a loop.

    In essence, make a MasterFilter, with a LogicalOperator of type 'OR'. Then inside that place a sub-filterexpression that is of type AND for things like Country, Company, etc. I also have one more sub Filterexpression if the region contains a zipcode range (It AND's the bottom and top zipcode values with a greaterequal and lessequal condition).

    How do I dynamically create this series of FilterExpressions? It seems they would need unique names in order to add them to the masterfilter which resides outside the loop. But C# doesn't seem to support dynamic variable names, so I have no way to make these distinct? I know I'm  probably being silly about this, any thoughts?

    Monday, March 28, 2011 3:44 PM

Answers

  • This might help you:-

    QueryExpression query = new QueryExpression();
    
    query.EntityName="account";
    
    ColumnSet columns = new ColumnSet();
    columns.Attributes = new string[] { "accountnumber","name" };
    query.ColumnSet = columns;
    
    
    LinkEntity linkEntity1 = new LinkEntity();
    linkEntity1.JoinOperator = JoinOperator.Natural;
    linkEntity1.LinkFromEntityName = "account";
    linkEntity1.LinkFromAttributeName = "territoryid";
    linkEntity1.LinkToEntityName = "territory";
    linkEntity1.LinkToAttributeName = "territoryid";
    
    linkEntity1.LinkCriteria = new FilterExpression();
    linkEntity1.LinkCriteria.FilterOperator = LogicalOperator.And;
    
    
    ConditionExpression condition1 = new ConditionExpression();
    condition1.AttributeName = "name";
    condition1.Operator = ConditionOperator.Equal;
    condition1.Values = new object[] { "Wales" };
    
    
    ConditionExpression condition2 = new ConditionExpression();
    condition2.AttributeName = "modifiedbyname";
    condition2.Operator = ConditionOperator.Equal;
    condition2.Values = new object[] { "Faisal" };
    
    
    query.Criteria.Conditions = new ConditionExpression[] { condition1, condition2 };
    
    
    linkEntity1.LinkCriteria = new FilterExpression();
    linkEntity1.Criteria.FilterOperator = LogicalOperator.Or;
    
    
    ConditionExpression condition3 = new ConditionExpression();
    condition3.AttributeName = "description";
    condition3.Operator = ConditionOperator.Equal;
    condition3.Values = new object[] { "Highland" };
    
    
    query.Criteria.Conditions = new ConditionExpression[] { condition3 };
    
    
    
    query.LinkEntities = new LinkEntity[] { linkEntity1 };
    

    Regards Faisal
    • Marked as answer by nickpeterson Monday, March 28, 2011 8:07 PM
    Monday, March 28, 2011 4:15 PM

All replies

  • This might help you:-

    QueryExpression query = new QueryExpression();
    
    query.EntityName="account";
    
    ColumnSet columns = new ColumnSet();
    columns.Attributes = new string[] { "accountnumber","name" };
    query.ColumnSet = columns;
    
    
    LinkEntity linkEntity1 = new LinkEntity();
    linkEntity1.JoinOperator = JoinOperator.Natural;
    linkEntity1.LinkFromEntityName = "account";
    linkEntity1.LinkFromAttributeName = "territoryid";
    linkEntity1.LinkToEntityName = "territory";
    linkEntity1.LinkToAttributeName = "territoryid";
    
    linkEntity1.LinkCriteria = new FilterExpression();
    linkEntity1.LinkCriteria.FilterOperator = LogicalOperator.And;
    
    
    ConditionExpression condition1 = new ConditionExpression();
    condition1.AttributeName = "name";
    condition1.Operator = ConditionOperator.Equal;
    condition1.Values = new object[] { "Wales" };
    
    
    ConditionExpression condition2 = new ConditionExpression();
    condition2.AttributeName = "modifiedbyname";
    condition2.Operator = ConditionOperator.Equal;
    condition2.Values = new object[] { "Faisal" };
    
    
    query.Criteria.Conditions = new ConditionExpression[] { condition1, condition2 };
    
    
    linkEntity1.LinkCriteria = new FilterExpression();
    linkEntity1.Criteria.FilterOperator = LogicalOperator.Or;
    
    
    ConditionExpression condition3 = new ConditionExpression();
    condition3.AttributeName = "description";
    condition3.Operator = ConditionOperator.Equal;
    condition3.Values = new object[] { "Highland" };
    
    
    query.Criteria.Conditions = new ConditionExpression[] { condition3 };
    
    
    
    query.LinkEntities = new LinkEntity[] { linkEntity1 };
    

    Regards Faisal
    • Marked as answer by nickpeterson Monday, March 28, 2011 8:07 PM
    Monday, March 28, 2011 4:15 PM
  • I appreciate the example, I reformed my query a bit and ran than entire query in a loop rather than trying to programmatically build it, it just ended up to messy to build the large query compared with running 4-6 queries in a row and saving the results to a list.
    Monday, March 28, 2011 8:07 PM