Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Multiple linked Entity query CRM

(0) ShareShare
ReportReport
Posted on by

Hi Experts 

I am new to crm 

I have a team entity that is linked the equipment entity (equipment entity are a lookup field to the team), then under the team i have a calendar linked to it.

Team > Equipment (TeamLookup)> Calendar 

I have written a QueryExpression to retrive the calendarID linked to the equipment below 

Guid TeamId = this.inputTeam.Get(executionContext).Id;

QueryExpression query = new QueryExpression("team") { ColumnSet = new ColumnSet("teamid") };
LinkEntity linkedEquipment = query.AddLink("equipment", "teamid", "new_team");
LinkEntity linkedCalender = linkedEquipment.AddLink("calendar", "calendarid", "calendarid");
query.Criteria.AddCondition("pro_team", ConditionOperator.Equal, TeamId);
EntityCollection results = service.RetrieveMultiple(query);

if (results.Entities.Count > 0)
{
Entity Resource = results.Entities[0];
currentResourceId = Resource.GetAttributeValue<Guid>("calendarid");
}

return currentResourceId;


}

However the below doesn't return an calendarID I get new_Team field doesnot exits on the Team Entity error 

*This post is locked for comments

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: Multiple linked Entity query CRM

    Hi,

    Please try using the code below.

                Guid TeamId = this.inputTeam.Get(executionContext).Id;
                QueryExpression query = new QueryExpression()
                {
                    EntityName = "new_equipment", //Replace with the logical name of the equipment entity
                    ColumnSet = new ColumnSet("new_team"), //Replace with the logical name of the team lookup field in equipment entity
                };
    
    
                var linkedTeam = new LinkEntity
                {
                    LinkFromEntityName = "new_equipment", //Replace with the logical name of the equipment entity
                    LinkToEntityName = "team", //Replace with the logical name of the team entity
                    LinkFromAttributeName = "new_team", //Replace with the logical name of the team lookup field in equipment entity
                    LinkToAttributeName = "teamid", //Replace with the logical name of team GUID in team entity
                    JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                    EntityAlias = "Team",
                    Columns = new ColumnSet("calendarid") //Replace with the logical name of the calendar lookup in team entity
                };
    
                linkedTeam.LinkCriteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, TeamId)); //Replace with the logical name of team GUID in team entity
                query.LinkEntities.Add(linkedTeam);
    
                EntityCollection results = service.RetrieveMultiple(query);
                if (results.Entities.Count > 0)
                {
                    Entity Resource = results.Entities[0];
                    currentResourceId = ((EntityReference)((Resource.Attributes["Team.calendarid"] as AliasedValue).Value)).Id; ////Replace with the logical name of the calendar lookup in team entity
                }
                return currentResourceId;

    Hope this helps.

  • Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: Multiple linked Entity query CRM

    Hi,

    Below is an example of multiple levels of linked entities. Try and modify this to work with your entities.

    Replace role with team, systemuserroles with equipment and systemuser with calendar.

           public bool IsUserInRole(Guid userId, Guid roleId)

           {

               LinkEntity systemUserLink = new LinkEntity()

               {

                   LinkFromEntityName = "systermuserroles",

                   LinkFromAttributeName = "systemuserid",

                   LinkToEntityName = "systemuser",

                   LinkToAttributeName = "systemuserid",

                   LinkCriteria =

                   {

                       Conditions =

                       {

                           new ConditionExpression("systemuserid", ConditionOperator.Equal, userId)

                       }

                   }

               };

               QueryExpression query = new QueryExpression()

               {

                   EntityName = "role",

                   ColumnSet = new ColumnSet("roleid"),

                   LinkEntities =

                   {

                       new LinkEntity()

                       {

                           LinkFromEntityName = "role",

                           LinkFromAttributeName = "roleid",

                           LinkToEntityName = "systemuserroles",

                           LinkToAttributeName = "roleid",

                           LinkEntities = {systemUserLink}

                       }

                   },

                   Criteria =

                   {

                       Conditions =

                       {

                           new ConditionExpression("roleid", ConditionOperator.Equal, roleId)

                       }

                   }

               };

               EntityCollection results = OrganizationService.RetrieveMultiple(query);

               bool rc = (results.Entities.Count > 0);

               return rc;

           }

    Hope this helps.

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans