web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

How to check if account has child accounts

(0) ShareShare
ReportReport
Posted on by 5

Hi,

I'm using c# to retrieve data from dynamics.

I have a list of accounts and I need to retrieve only those that don't have child accounts.

is there a field name like for example:

accountQuery.ColumnSet.AddColumns("name", "parentaccountid","statuscode");

that I can use to check if the account has child accounts?

If not, what are my options?

Thanks!!!

I have the same question (0)
  • Itai m Profile Picture
    5 on at
    RE: How to check if account has child accounts

    Exception:System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: The entity with a name = 'childaccount' with namemapping = 'Logical' was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=114852495, MinActiveRowVersion=114852495, MetadataInstanceId=42685471 (Fault Detail is equal to Exception details:

    ErrorCode: 0x80040265

    Message: The entity with a name = 'childaccount' with namemapping = 'Logical' was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=114852495, MinActiveRowVersion=114852495, MetadataInstanceId=42685471

    TimeStamp: 2020-10-18T15:09:40.3444441Z

    --

    Exception details:

    ErrorCode: 0x80040265

    Message: The entity with a name = 'childaccount' with namemapping = 'Logical' was not found in the MetadataCache. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=114852495, MinActiveRowVersion=114852495, MetadataInstanceId=42685471

    TimeStamp: 2020-10-18T15:09:40.3444441Z

    --

  • Itai m Profile Picture
    5 on at
    RE: How to check if account has child accounts

    I`m trying to use link entity instead of the xml, but I'm getting exception in the last row.

    var accountQuery = new QueryExpression("account");

    accountQuery.ColumnSet.AddColumns("name", "parentaccountid",

                                                                 "parentaccountid","statuscode", "accountid");

               LinkEntity entity = new LinkEntity()

               {

                   LinkFromEntityName = "account",

                   LinkFromAttributeName = "parentaccountid",

                   LinkToEntityName = "childaccount",

                   LinkToAttributeName = "accountid",

                   JoinOperator = JoinOperator.LeftOuter,

                   Columns = new ColumnSet("name", "statuscode"),

                   EntityAlias = "childaccount"

               };

               accountQuery.LinkEntities.Add(entity);

               FilterExpression Filter1 = new FilterExpression(LogicalOperator.Or);

               Filter1.AddCondition("statuscode", ConditionOperator.Equal, _liveStatusCode);

               Filter1.AddCondition("statuscode", ConditionOperator.Equal, _implemantation);

               FilterExpression Filter2 = new FilterExpression();

               Filter2.AddCondition("childaccount", ConditionOperator.Null, null);

               FilterExpression mainFilter = new FilterExpression(LogicalOperator.And);

               mainFilter.AddFilter(Filter1);

               mainFilter.AddFilter(Filter2);

               accountQuery.Criteria = mainFilter;

               EntityCollection accountResult = organizationService.RetrieveMultiple(accountQuery);

  • Suggested answer
    faheem123 Profile Picture
    107 on at
    RE: How to check if account has child accounts

    Hi,

    You can try below fetchxml to retrieve the accounts with no child records.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

    <entity name="account">

      <attribute name="name" />

      <attribute name="accountid" />

       <filter type="and">

          <condition entityname="childaccount" attribute="accountid" operator="null" />

      </filter>

      <link-entity name="account" alias="childaccount" from="parentaccountid" to="accountid" link-type='outer'>

      </link-entity>

    </entity>

    </fetch>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 182

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 129 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans