locked
Retrieve Lookup value of primary contact for account entity. RetrieveMultiple, Query Expression RRS feed

  • Question

  • qe.LinkEntities.Add(new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner));
                qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname");
                qe.LinkEntities[0].EntityAlias = "primarycontact";

                EntityCollection ec = crmService.RetrieveMultiple(qe);

                Console.WriteLine("Retrived {0} entities", ec.Entities.Count);
                Console.ReadLine();

                foreach (Entity act in ec.Entities)
                {
                    Console.WriteLine("account name:" + act["name"]);
                    Console.WriteLine("primary contact first name:" + /*act["primarycontact.firstname"].ToString()*/);
                    Console.WriteLine("primary contact last name:" + ???);
                }

    How to retrieve the primary contact for respective account? 

    Wednesday, November 15, 2017 11:40 AM

All replies

  •                     Entity account = new Entity("account");
                        ColumnSet cols = new ColumnSet(new String[] { "accountid", "name", "primarycontactid" });
                        account = _service.Retrieve(account.LogicalName, _accountId, cols);
                        EntityReference pContact = (EntityReference)account["primarycontactid"];
                        string contactName = pContact.Name;

    Wednesday, November 15, 2017 11:57 AM
  • Thanks for your quick response. But my requirement is to use LinkEntity and JoinOperator so, how can i do the same using this?
    Wednesday, November 15, 2017 12:07 PM
  •                     //Create Query Expression.
                        QueryExpression query = new QueryExpression()
                        {
                            EntityName = "role",
                            ColumnSet = new ColumnSet("name"),
                            LinkEntities = 
                            {
                                new LinkEntity
                                {
                                    LinkFromEntityName = Role.EntityLogicalName,
                                    LinkFromAttributeName = "roleid",
                                    LinkToEntityName = SystemUserRoles.EntityLogicalName,
                                    LinkToAttributeName = "roleid",
                                    LinkCriteria = new FilterExpression
                                    {
                                        FilterOperator = LogicalOperator.And,
                                        Conditions = 
                                        {
                                            new ConditionExpression
                                            {
                                                AttributeName = "systemuserid",
                                                Operator = ConditionOperator.Equal,
                                                Values = { _userId }
                                            }
                                        }
                                    }
                                }
                            }
                        };
    
                        // Obtain results from the query expression.
                        EntityCollection ec = _serviceProxy.RetrieveMultiple(query);
    
                        // Display results.
                        for (int i = 0; i < ec.Entities.Count; i++)
                        {
                            Console.WriteLine("Query Expression Retrieved: {0}", ((Role)ec.Entities[i]).Name);
                        }

    Wednesday, November 15, 2017 1:28 PM
  • what is  Role.EntityLogicalName? I am trying to do like contact.EntityLogical?Name it is not accepting.

    also you have used LinkFromEntityName="roleid" twice so i am confused whether i have to write the "primarycontactid" in place of "roleid" for "contact"and "account" entity 

    Wednesday, November 15, 2017 2:42 PM
  • I think you like ready made food. Just change the names of entities and attributes. This is an example 

    Regards Faisal

    Wednesday, November 22, 2017 11:37 AM
  • You can use below FetchXML to retrieve the primary contact for respective account. You can also translate fetchXML into query expression.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
      <entity name="contact" >
        <attribute name="fullname" />
        <attribute name="address1_name" />
        <attribute name="address1_city" />
        <attribute name="lastname" />
        <attribute name="address1_country" />
        <attribute name="firstname" />
        <link-entity name="account" from="accountid" to="parentcustomerid" link-type="inner" >
          <filter type="and" >
            <condition attribute="accountid" operator="eq" value="da2e7449-74a1-4296-8ed5-9514571c18db" />
          </filter>
        </link-entity>
      </entity>
    </fetch>


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". CRMHUNT http://crmhunt.com

    Friday, December 1, 2017 9:09 AM
  • You have to use the AliasedValue:

    foreach (Entity act in ec.Entities

    {
        Console.WriteLine("account name:" + act["name"]);
        Console.WriteLine("primary contact first name:" + ((AliasedValue)act["primarycontact.firstname"]).Value.ToString());
        Console.WriteLine("primary contact last name:" + ((AliasedValue)act["primarycontact.lastname"]).Value.ToString());

    }

    Sunday, December 3, 2017 8:02 PM