Search code examples
dynamics-crmmicrosoft-dynamics

What is entity reference and Query Expression ? Please give some simple examples


What is EntityReference and QueryExpression? Please give me some simple examples.


Solution

  • EntityReference

    It is used for lookup fields in 365 – e.g. to link records via a 1 to many relationship. The lookup field is shown on the ‘child’. You need to specify the ‘parent’ entity type and record id.

    For example if you are creating a accountand want to set the primary contact.

    Entity account = new Entity("account"); 
    account["name"] = "James Account"; 
    account["primarycontactid"] = new EntityReference("contact", contactId);
    service.Create(account);
    

    QueryExpression

    QueryExpression provides an object model to construct a query. Queries can also be created using FetchXML, a proprietary XML based query language.

    For example of you wanted to retrieve all contacts full name and telephone number.

    QueryExpression query = new QueryExpression()
    {
        Distinct = false,
        EntityName = Contact.EntityLogicalName,
        ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
    };
    DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;