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
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.
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.
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6