Professional Documents
Culture Documents
FetchXML - Complete Reference - MSXRMTOOLS
FetchXML - Complete Reference - MSXRMTOOLS
MSXRMTOOLS Tools MCQ Q&A About Contact Dynamics 365 Training Online Log in Register
;
FetchXML can be used for retrieving data from Microsoft Dynamics CRM. Filter Records Based On
FetchXML cannot be used for Create, Update and Delete operations. • Related Entity
(/fetchxml/reference#3)
FetchXML can perform aggregations like Sum, Average, Minimum, Maximum,
Count(*), Count(attributename)
Complete List Of
FetchXML query can do GoupBy, Sorting and Filter on linked entity.
FetchXML queries are defined in XML format.
• FetchXML Operators
(/fetchxml/reference#4)
FetchXML query can retrieve upto 5000 records in a single request.
FetchXML allows paging results using paging cookie. Limit Maximum Number
FetchXML can be saved as user-owned view in the userquery entity and as an • Of Records Returned
(/fetchxml/reference#5)
organization-owned view in the savedquery entity.
Explanation · Aggregates
(/fetchxml/reference#
<all-attributes /> clause makes FetchXML query to retrive all attributes for an Count
entity.
· Aggregate
(/fetchxml/reference#
As a best-practice retrieve only the required attributes for better performance. Retrieving
selected attributes is explained below. Countcolumn
· Aggregate
• (/fetchxml/reference#
·
2. <entity name='account' > Min Aggregate
3. <attribute name='accountid' /> (/fetchxml/reference#
4. <attribute name='name' />
5. </entity> · Sum Aggregate
(/fetchxml/reference#
6. </fetch>
Multiple
Explanation
· Aggregates
http://msxrmtools.com/fetchxml/reference 1/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
FetchXML query will return all the records if no filter applied. (/fetchxml/reference#
To retrieve the selected attributes for an entity specify each attribute separately.
Group By
<attribute name='accountid' /> (/fetchxml/reference#18)
Group by with
<attribute name='name' />
· linked entity
(/fetchxml/reference#
· Group By Year
•
name of the owning user is not equal to Cannon: (/fetchxml/reference#
1. mapping='logical' >
· Group By Month
<fetch
2. <entity name='account' > (/fetchxml/reference#
·
3. <attribute name='accountid' /> Group By Week
4. <attribute name='name' /> (/fetchxml/reference#
5. <link-entity name='systemuser' to='owninguser' >
6.
7.
<filter type='and' >
<condition attribute='lastname' operator='ne' value='Ca
· Group By Day
(/fetchxml/reference#
8. Multiple group
·
</filter>
9. </link-entity> by
10. </entity> (/fetchxml/reference#
• Order
11. </fetch>
by
(/fetchxml/reference#26)
Explanation
Fiscal Date in FetchXML
To filter entity records add <link-entity> clause, ex <link-entity
(/fetchxml/reference#27)
name='systemuser' to='owninguser'> . Where name attributes contains linked
entity logical name and to attribute contains logical name of attribute in main entity Last Fiscal
which is linked to related entity
Specify filter clause within link-entity.
· Period
FetchXML Query
Within filter specify the filter criteria using <condition attribute='lastname' (/fetchxml/reference#
operator='ne' value='Cannon' /> . Where attribute contains logical name of In Fiscal Year
attribute in related entity, operator defines the comparison type here ne means
not-equal and value defines the value to compare.
· FetchXML Query
(/fetchxml/reference#
Complete list of FetchXML operators is defined below. • in-fiscal-period
· FetchXML Query
(/fetchxml/reference#
eq Equals
ne Not Equals
http://msxrmtools.com/fetchxml/reference 2/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
in Is In
not-in Is Not In
between Between
like Like
yesterday Yesterday
today Today
tomorrow Tomorrow
on On
on-or-before On or Before
on-or-after On on After
http://msxrmtools.com/fetchxml/reference 3/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
http://msxrmtools.com/fetchxml/reference 4/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
In the below example, the FetchXML finds all accounts that have no leads
Explanation
You can perform a left outer join in FetchXML by using the entityname attribute as a
condition operator. The entityname attribute is valid in conditions, filters, and nested
filters.
To perform left outer join in FetchXML query set following attributes in link-entity
clause.
Set name attribute to logical name of related entity.
Set to attribute to linking attribute in primary entity.
Set from attribute to linking attribute in related entity.
http://msxrmtools.com/fetchxml/reference 5/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Aggregates in FetchXML
In Microsoft Dynamics CRM FetchXML includes grouping and aggregation features that let
you calculate sum, average, minimum, maximum and count.
sum
avg
min
max
count(*)
count(attribute name)
About Aggregation
To create an aggregate attribute,
Average Aggregates
Following FetchXML calculates average of estimated value from opportunity.
Explanation
To calculate average set following:
http://msxrmtools.com/fetchxml/reference 6/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Opportunity 3 Account 2 0
Count Aggregate
The following FetchXML counts opportunity records
Explanation
To calculate count set following:
Countcolumn Aggregate
Countcolumn aggregate counts records where specified column don’t have null.
http://msxrmtools.com/fetchxml/reference 7/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To use countcolumn aggregate
Explanation
To count distinct column values
Max Aggregate
Max aggregate returns the maximum value in a specified column.
Null values are not considered when Microsoft Dynamics CRM computes the
maximum of data. However, zero (0) is used.
The following FetchXML shows how to get the maximum value from estimated
column of opportunity entity.
Explanation
To get maximum value from a column set:
Min Aggregate
Min aggregate returns the minimum value in a specified column.
http://msxrmtools.com/fetchxml/reference 8/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Null values are not considered when Microsoft Dynamics CRM computes the
minimum of data. However, zero (0) is used.
The following FetchXML shows how to get the maximum value from estimated
column of opportunity entity.
Explanation
To get minimum value from a column set:
Sum Aggregate
The following FetchXML calculates the sum of estimated value from
opportunity entity.
Explanation
To get the sum of values in a column:
Multiple Aggregates
Multiple aggregates can be computed in a single FetchXML query.
Following FetchXML calculates count, sum and avg from opportunity entity.
Explanation
To calculate multiple aggregate in single FetchXML query
http://msxrmtools.com/fetchxml/reference 9/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Group By
Aggregates can be grouped in FetchXML, to group aggregates set groupby attribute to
true.
Explanation
To group aggregates in FetchXML query
Explanation
To create group by with linked entity FetchXML
http://msxrmtools.com/fetchxml/reference 10/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Group By Year
Group By for dates uses the day, week, month, quarter or year value.
The following FetchXML query shows how to use the aggregate attribute and
the groupby attribute to group the results by year.
Explanation
To create group by Year FetchXML query:
Group By Quarter
http://msxrmtools.com/fetchxml/reference 11/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
The following example shows how to use the aggregate attribute and the
groupby attribute to group the results by quarter.
Explanation
To create group by Quarter FetchXML query:
Group By Month
The following FetchXML shows how to use the aggregate attribute and the
groupby attribute to group the results by month.
http://msxrmtools.com/fetchxml/reference 12/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create group by Month FetchXML query:
Group By Week
The following FetchXML shows how to use the aggregate attribute and the
groupby attribute to group the results by week.
http://msxrmtools.com/fetchxml/reference 13/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create group by Week FetchXML query:
Group By Day
The following FetchXML shows how to use the aggregate attribute and the
groupby attribute to group the results by week.
http://msxrmtools.com/fetchxml/reference 14/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create group by Day FetchXML query:
Multiple group by
The following FetchXML shows how to use the aggregate attribute and
multiple groupby clauses.
http://msxrmtools.com/fetchxml/reference 15/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create multiple group by FetchXML query:
Order by
The following FetchXML query shows how to use the aggregate attribute and multiple
orderby clauses.
http://msxrmtools.com/fetchxml/reference 16/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create order by FetchXML query:
Add order clause in entity and set descending keyword to true or false.
For all fiscal date queries, the FetchXML query uses the organization’s fiscal year settings.
For example, if the organization uses fiscal months, the query returns orders
fulfilled in the last fiscal month. If the organization uses fiscal quarters, the
query returns orders fulfilled in the last fiscal quarter. If the organization uses
fiscal semesters, orders fulfilled in the last fiscal semester are returned.
1. <fetch>
2. <entity name="order" >
3. <attribute name="name" />
4. <filter type="and" >
5. <condition attribute="datefulfilled" operator="last-fis
/>
6. </filter>
7. </entity>
8. </fetch>
http://msxrmtools.com/fetchxml/reference 17/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Explanation
To create last fiscal period FetchXML query:
1. <fetch>
2. <entity name="account" >
3. <attribute name="name" />
4. <filter type="and" >
5. <condition attribute="createdon" operator="in-fiscal-ye
/>
6. </filter>
7. </entity>
8. </fetch>
Explanation
To create In Fiscal Year FetchXML query:
next-x-fiscal-years Query
The following example shows a FetchXML expression that finds all
opportunities with an estimated close date in the next three fiscal years, based
on the organization’s fiscal year settings.
1. <fetch>
2. <entity name="opportunity" >
3. <attribute name="name" />
4. <filter type="and" >
5. <condition attribute="estimatedclosedate" operator="nex
/>
6. </filter>
7. </entity>
8. </fetch>
Explanation
To create next-x-fiscal-years FetchXML query:
http://msxrmtools.com/fetchxml/reference 18/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
1. <fetch>
2. <entity name="order" >
3. <attribute name="name" />
4. <filter type="and" >
5. <condition attribute="datefulfilled" operator="in-fisca
/>
6. </filter>
7. </entity>
8. </fetch>
Explanation
To create in-fiscal-period FetchXML query:
in-fiscal-period-and-year Query
The following example shows a FetchXML expression that finds all orders
fulfilled in period three of fiscal year 2013, according to the organization’s fiscal
year settings. If the organization uses fiscal months, the query returns results
from month three. If the organization uses fiscal quarters, the query returns
results from quarter three. If the organization uses fiscal semesters, no results
are returned; there are only two semesters, and the value supplied is therefore
out-of-range.
1. <fetch>
2. <entity name="order" >
3. <attribute name="name" />
4. <filter type="and" >
5. <condition attribute="datefulfilled" operator="in-fisca
>
6. <value> 3 </value>
7. <value> 2013 </value>
8. </condition>
9. </filter>
10. </entity>
11. </fetch>
Explanation
To create in-fiscal-period-and-year FetchXML query:
<value> 3 </value>
Explanation
To create group by with linked entity FetchXML:
1. <fetch>
2. <entity name="incident" >
3. <attribute name="title" />
4. <attribute name="ticketnumber" />
5. <attribute name="createdon" />
6. <attribute name="incidentid" />
7. <filter type="and" >
8. <condition attribute="createdon" operator="olderthan
/>
9. </filter>
10. </entity>
11. </fetch>
http://msxrmtools.com/fetchxml/reference 20/21
4/3/2018 FetchXML - Complete Reference - MSXRMTOOLS
Older <condition attribute=" <AttributeName> " operator="olderthan-x-days" value=" <VALUE> " />
than X
days
Older <condition attribute=" <AttributeName> " operator="olderthan-x-weeks" value=" <VALUE> " />
than X
weeks
Older <condition attribute=" <AttributeName> " operator="olderthan-x-months" value=" <VALUE> " />
than X
months
Older <condition attribute=" <AttributeName> " operator="olderthan-x-years" value=" <VALUE> " />
than X
years
© 2018 - MSXRMTOOLS
http://msxrmtools.com/fetchxml/reference 21/21