Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to check if account has child accounts

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!!!

  • Itai m Profile Picture
    Itai m 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
    Itai m 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
    faheem123 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans