1

I'm using Query Expression to retrieve related records through N:N relationship between 2 entities (User and Engagement)

But when I proceed with return data from query. the method entity.Attributes["systemuserid"] always return blank although it has data.

Here is my code:

 QueryExpression query = new QueryExpression(CrmAttributes.EntityName.User);
            query.ColumnSet = new ColumnSet(CrmAttributes.SystemUserAttributes.UserId);
            LinkEntity linkEntityFrom = new LinkEntity(CrmAttributes.EntityName.User,
                CrmAttributes.EntityRelationships.EngagementToSystemUser,
                CrmAttributes.SystemUserAttributes.UserId, CrmAttributes.SystemUserAttributes.UserId,
                JoinOperator.Inner);
            LinkEntity linkEntityTo = new LinkEntity(CrmAttributes.EntityRelationships.EngagementToSystemUser,
                CrmAttributes.EntityName.Engagement,
                CrmAttributes.EngagementAttributes.EngagementId, CrmAttributes.EngagementAttributes.EngagementId,
                JoinOperator.Inner);
            linkEntityFrom.LinkEntities.Add(linkEntityTo);
            query.LinkEntities.Add(linkEntityFrom);
            linkEntityFrom.LinkCriteria = new FilterExpression();
            linkEntityFrom.LinkCriteria.AddCondition(
                new ConditionExpression(CrmAttributes.EngagementAttributes.EngagementId, ConditionOperator.Equal, id));
            var results = service.RetrieveMultiple(query);


 foreach (var entity in results.Entities)
      { 
            if(entity.Attributes.Contains(CrmAttributes.SystemUserAttributes.UserId)) {
                Guid userGuid = entity.Attributes[CrmAttributes.SystemUserAttributes.UserId]  //systemuserid
                tracer.Trace("Guid: " + userGuid);      // Always blank 
           }
       }

But when I use KeyValuePair to get data. The data is exactly what I want

 foreach (var entity in results.Entities)
                { 
                    Guid userGuid = new Guid();
                    foreach (KeyValuePair<string, object> attribute in entity.Attributes)
                    {
                        if (attribute.Key == CrmAttributes.SystemUserAttributes.UserId)
                        {
                            userGuid = (Guid)attribute.Value;
                            tracer.Trace("Guid: " + userGuid); //Return expected data
                        }
                    }
                }

2 Answers2

0

Are you certain systemuserid is the correct field name to use for this query?

When joins are involved, CRM will often use a different attribute name as many entities could have fields with the same name.

For example it could be something like user1.systemuserid.

I would suggest debugging the code to review the attributes returned by the query - I suspect you will find the information you require.

James Wood
  • 17,286
  • 4
  • 46
  • 89
0

After a day leave this problem alone. It run perfectly now without any modification was made... Maybe something went wrong on the SDK or my CRM server and it took me a day to workaround with no answer @@.