locked
Get an account, all its parent accounts and all its child accounts using query expression in CRM 2015 RRS feed

  • Question

  • Hi,

    I'm very new to CRM development. I want to get the details of an account, all of its parent accounts and all of its child accounts using query expression. Can someone please help me with the C# code using query expression? This needs to be a recursive query expression.

    Previously I've used CTE in SQL to solve this problem.

                                   Account A

    Account B     Account C      Account D       Account E

    Account F

    Account G

    For example, if I pass Account Id of F, I should get full hierarchy tree. Here Account B, C, D and E are child of A, F is child of B and G is child of F.

    Cheers

    Ankit


    • Edited by ank_p_2002 Thursday, September 10, 2015 11:47 PM
    Thursday, September 10, 2015 11:21 PM

Answers

  • Got it working. Here is what I did:

    Somehow I managed to get the account id of the root account.

    Then I wrote a query expression to get all the accounts under the this root account.

    I got all the accounts present in the hierarchy.

    Thanks

    Ankit

    • Marked as answer by ank_p_2002 Thursday, September 17, 2015 10:43 PM
    Thursday, September 17, 2015 10:43 PM
  • Hello,

    The easiest way I see is usage of hierarchical queries that are available in CRM. One prerequisite is building and usage for filtering of OOB hierarchy. Recheck following articles:

    https://technet.microsoft.com/en-us/library/dn832068.aspx#BKMK_Querydata

    To fetch data you will have to use hierarchical condition operators.


    Dynamics CRM MVP
    My blog

    • Marked as answer by ank_p_2002 Thursday, September 17, 2015 10:43 PM
    Friday, September 11, 2015 5:17 AM
    Moderator

All replies

  • Hello,

    The easiest way I see is usage of hierarchical queries that are available in CRM. One prerequisite is building and usage for filtering of OOB hierarchy. Recheck following articles:

    https://technet.microsoft.com/en-us/library/dn832068.aspx#BKMK_Querydata

    To fetch data you will have to use hierarchical condition operators.


    Dynamics CRM MVP
    My blog

    • Marked as answer by ank_p_2002 Thursday, September 17, 2015 10:43 PM
    Friday, September 11, 2015 5:17 AM
    Moderator
  • Hi,

    Thanks for the reply. One question here I want to ask is, does these operators work when I use the CRM service in my C# code? I read somewhere that currently there is a restriction in CRM online that these Hierarchical condition operators are still not supported in CRM Online 2015 service which is consumed by the web application. I've written this code based on condition operators:

    QueryExpression

    query = newQueryExpression("account");

                query.ColumnSet.AddColumns(

    "accountid", "name", "parentaccountid");

               

    FilterExpressionexpression = newFilterExpression();

                expression.AddCondition(

    newConditionExpression("accountid", ConditionOperator.AboveOrEqual, accountGuid));

                expression.AddCondition(

    newConditionExpression("accountid", ConditionOperator.Under, accountGuid));

    expression.FilterOperator =

    LogicalOperator.Or;

    I need to test this code though.

    Thanks

    Ankit

    Friday, September 11, 2015 4:56 PM
  • Could you please provide an article that states that usage of mentioned approach is not valid? As far as I know it is valid and works.

    Dynamics CRM MVP
    My blog

    Friday, September 11, 2015 8:07 PM
    Moderator
  • Actually this works as the endpoint I'm using is not an OData endpoint. This approach will not work for OData endpoint.

    However, I'm still not getting the desired results. If I pass the account Id of Account G, then I should get the full hierarchy right from account A to G including accounts C, D and E. So I believe, I need to write a loop which says that

    for all accounts.aboveOrEqual

    {

    get child accounts()

    add to hierarchy()

    }

    Tuesday, September 15, 2015 7:51 PM
  • Got it working. Here is what I did:

    Somehow I managed to get the account id of the root account.

    Then I wrote a query expression to get all the accounts under the this root account.

    I got all the accounts present in the hierarchy.

    Thanks

    Ankit

    • Marked as answer by ank_p_2002 Thursday, September 17, 2015 10:43 PM
    Thursday, September 17, 2015 10:43 PM