Understanding FetchXML: Breaking Down a Query Example

FetchXML is a powerful XML-based query language for retrieving data from Microsoft Dataverse. Let’s break down a sample FetchXML query and understand its components.


FetchXML Query

<fetch>
  <entity name='contact'>
    <attribute name='fullname' />
    <attribute name='emailaddress1' />
    <link-entity name='account' from='accountid' to='parentcustomerid' alias='account'>
      <filter>
        <condition attribute='accountid' operator='eq' value='ACCOUNT_ID' />
      </filter>
    </link-entity>
  </entity>
</fetch>

Explanation of Components

  1. <fetch>: The root element of the query, containing the definition of what data to retrieve.

  2. <entity>: Defines the primary entity for the query. In this example:

    • name='contact': Specifies the contact entity as the target.
  3. <attribute>: Specifies which fields (attributes) to retrieve from the contact entity.

    • name='fullname': Retrieves the fullname attribute.
    • name='emailaddress1': Retrieves the emailaddress1 attribute.
  4. <link-entity>: Joins related entities to include data from another table. Here:

    • name='account': Joins the account entity.
    • from='accountid': The key in the account entity used for the join.
    • to='parentcustomerid': The key in the contact entity to match with the accountid.
    • alias='account': Provides an alias for the linked entity.
  5. <filter>: Defines criteria to filter the data from the linked account entity. Here:

    • attribute='accountid' operator='eq' value='ACCOUNT_ID': Filters the query to only include records where the accountid matches the specified value (ACCOUNT_ID).

What This Query Does

  • Target Entity: The query starts by focusing on the contact entity.
  • Attributes to Retrieve: It retrieves the fullname and emailaddress1 fields of the contact records.
  • Join Operation: It links the account entity to filter contacts based on related accounts.
  • Filter Condition: Only includes contacts where the accountid of the account entity matches the specified value (ACCOUNT_ID).

Result

The result of this FetchXML query will be:

  • A collection of contact records.
  • Only contacts linked to the specified account (ACCOUNT_ID).
  • Each record will include the fullname and emailaddress1 attributes.

Conclusion

FetchXML provides a flexible and powerful way to query Dataverse data, allowing you to filter, join, and retrieve exactly the data you need. This breakdown of the sample query demonstrates how to leverage FetchXML for complex data retrieval scenarios.

Related Posts