Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

QueryExpression for all Related Entities

Posted on by 135

I am trying to build a QueryExpression in C# to get all related entities for a given parent entity.

Could you please help me with this task?
Thanks.

*This post is locked for comments

  • Suggested answer
    CRM-Dev Profile Picture
    CRM-Dev 135 on at
    RE: QueryExpression for all Related Entities

    I go it:

                   RetrieveEntityRequest retrieveBankAccountEntityRequest = new RetrieveEntityRequest

                   {

                       EntityFilters = EntityFilters.Relationships,

                       LogicalName = 'account'

                   };

                   RetrieveEntityResponse retrieveBankAccountEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveBankAccountEntityRequest);

                   var oneToNRelationships = retrieveBankAccountEntityResponse.EntityMetadata.OneToManyRelationships;

                   foreach (var oneToNRelationship in oneToNRelationships)

                   {

                       // your code

                   }

  • CRM-Dev Profile Picture
    CRM-Dev 135 on at
    RE: QueryExpression for all Related Entities

    I need to find the related entities not the records of the related entities.

  • Suggested answer
    Nuno Profile Picture
    Nuno 2,005 on at
    RE: QueryExpression for all Related Entities

    Hi,

    QueryExpression query = new QueryExpression("ChildEntityLogicalName");
    query.Criteria = new FilterExpression(LogicalOperator.And);
    query.Criteria.AddCondition(new ConditionExpression("AttributeChildEntityEntityReferenceToParentEntity", ConditionOperator.Equal, new Guid("GuidOfParententity")));
    


  • Inogic Profile Picture
    Inogic 24,088 on at
    RE: QueryExpression for all Related Entities

    If you want to retrieve all the related records then you can retrieve entity relationships using metadata.

    Then retrieve related records using each relationship.

  • Suggested answer
    Faizal Profile Picture
    Faizal 545 on at
    RE: QueryExpression for all Related Entities

    Hi CRM Dev,

    Did you mean to get ALL related entities for given parent entity?

    or ALL records in specified one/more related entity for given record in parent entity?

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,030 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans