Raman Sinclair
Raman Sinclair

Reputation: 1283

CRM : Linked Entities collection is empty

I have the following query to get account with related entity = primary contact. Executing this query I successfully get accounts, but related entity collection is empty.

    var _service = _getService();
    var q = new QueryExpression("account");
    q.ColumnSet = new ColumnSet("name");

    q.LinkEntities.Add(new LinkEntity("account", "contact", 
"primarycontactid", "contactid", JoinOperator.Inner));
    q.LinkEntities[0].Columns.AllColumns = true;
    q.LinkEntities[0].EntityAlias = "temp";

    EntityCollection ec = _service.RetrieveMultiple(q);

What am I doing wrong?

Update 1: to check related entities fields I use

for (int i = 0; i < ec.Entities.Count; i++)
{
    Console.Write(ec.Entities[i].RelatedEntities.Count + " ");
}

Upvotes: 1

Views: 543

Answers (1)

Andrew Butenko
Andrew Butenko

Reputation: 5446

Just to be clear when you join entities in that way all the related entity information (contact in your case) will be returned along with fields of primary entity (account in your case).

To get field from related entity you will have to check the same entity like (string)ec.Entities[0].GetAttributeValue<AliasedValue>("temp.fullname").Value

Upvotes: 2

Related Questions