locked
Query contacts based on account filter RRS feed

  • Question

  • My task: Create a function that retrieves all contacts linked to an account. The input to the function is account name.

    Note: I'm using the CRM webservices and it is CRM 4.0.

    I want to do this in one request if possible, but I can't figure out how to do it. I've tried to do a query expression using join, but it seems to only handle one entity type at the time, i.e. I cannot set a filter expression working on the account while retrieving columns from the contacts.

    So my question is: is there a way to do this in one request or do I need to first query the account to get its ID and then query the belonging contacts afterwards?

    Wednesday, January 19, 2011 12:40 PM

Answers

  • Have you tried with the new LINQ interface?

    AFAIR I could do that using LINQ.

    Hope it helps,

    PP


    Microsoft MVP Dynamics CRM | My Twitter: http://twitter.com/pabloperalta | My blog: http://weblogs.asp.net/pabloperalta
    • Marked as answer by maze_dk Thursday, January 20, 2011 10:45 AM
    Wednesday, January 19, 2011 2:08 PM

All replies

  • Have you tried with the new LINQ interface?

    AFAIR I could do that using LINQ.

    Hope it helps,

    PP


    Microsoft MVP Dynamics CRM | My Twitter: http://twitter.com/pabloperalta | My blog: http://weblogs.asp.net/pabloperalta
    • Marked as answer by maze_dk Thursday, January 20, 2011 10:45 AM
    Wednesday, January 19, 2011 2:08 PM
  • Hello maze_dk,

    Here is one way you could do it through FetchXML:

     <fetch mapping='logical'>
     <entity name='account'>
      <filter type='and'/>
      <filter type='and'>
       <condition attribute='accountid' operator='eq' value='1B12B39E-E38C-DA11-829C-0003FF020F6E'/>
      </filter>
      <link-entity name='contact' from='accountid' to='accountid' link-type='inner'>
       <attribute name='firstname'/>
       <attribute name='lastname'/>
      </link-entity>
     </entity>
    </fetch>

    Here is the code in C# (generated from the same FetchXML above):

    QueryExpression query = new QueryExpression();

    query.EntityName="account";

    ColumnSet columns = new ColumnSet();
    columns.Attributes = new string[] {  };
    query.ColumnSet = columns;


    query.Criteria = new FilterExpression();
    query.Criteria.FilterOperator = LogicalOperator.And;


    query.Criteria = new FilterExpression();
    query.Criteria.FilterOperator = LogicalOperator.And;


    ConditionExpression condition1 = new ConditionExpression();
    condition1.AttributeName = "accountid";
    condition1.Operator = ConditionOperator.Equal;
    condition1.Values = new object[] { "1B12B39E-E38C-DA11-829C-0003FF020F6E" };


    query.Criteria.Conditions = new ConditionExpression[] { condition1 };


    LinkEntity linkEntity1 = new LinkEntity();
    linkEntity1.JoinOperator = JoinOperator.Inner;
    linkEntity1.LinkFromEntityName = "account";
    linkEntity1.LinkFromAttributeName = "accountid";
    linkEntity1.LinkToEntityName = "contact";
    linkEntity1.LinkToAttributeName = "accountid";


    query.LinkEntities = new LinkEntity[] { linkEntity1 };

    The tool I used for this can be downloaded from:  http://jamesdowney.net/fetchxml/FetchXMLBuilder4.zip

    Good Luck!

    Oliver

     


    oliver barrera
    • Proposed as answer by oliver barrera Wednesday, January 19, 2011 5:36 PM
    Wednesday, January 19, 2011 5:36 PM
  • Thank you for your answer.

    I agree that it is fairly easy to do a query using a filter on "accountid", since this field exists in the contacts, but my question was how to do a query when the filter should work on a field in the account such as "name" and then output fields from the contacts.

    So to me it seems I have to split it into two queries, first getting the account by name to get the accountid, and then getting the associated contacts. This I can live with, but I was just wondering if it was possible to do in one query.

    Thursday, January 20, 2011 10:36 AM
  • Thanks for the answer regarding the new LINQ interface. I've read a bit about it and it seems that it can in fact do it in one query, though I'm not sure if it actually splits it into two queries behind the scenes. So I think I will experiment a little with the LINQ interface.
    Thursday, January 20, 2011 10:44 AM