FetchXML Query Examples
SELECT All Columns (*)
This example shows how to write simple FetchXML query to select all columns.
This FetchXML is the equivalent of below SQL
SELECT * FROM account
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<all-attributes />
</entity>
</fetch>
SELECT Specified Columns and Alias
This example shows how to write simple FetchXML query to select two columns and use alias for column name (e.g. name is renamed to accountname).
This FetchXML is the equivalent of below SQL
SELECT accountid, name as accountname
FROM account
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical'>
<entity name='account'>
<attribute name='accountid'/>
<attribute name='name' alias = 'accountname'/>
</entity>
</fetch>
WHERE (AND conditions)
This example shows how to write FetchXML query to filter data using multiple conditions (AND).
This FetchXML is the equivalent of below SQL
SELECT * FROM account WHERE revenue > 0 AND name like 'A%'
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<all-attributes />
<filter>
<condition attribute='revenue' operator='gt' value='0' />
<condition attribute='name' operator='like' value='A%' />
</filter>
</entity>
</fetch>
WHERE (OR conditions)
This example shows how to write FetchXML query to filter data using multiple conditions (OR).
This FetchXML is the equivalent of below SQL
SELECT * FROM account WHERE revenue > 0 OR name like 'A%'
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<all-attributes />
<filter type='or'>
<condition attribute='revenue' operator='gt' value='0' />
<condition attribute='name' operator='like' value='A%' />
</filter>
</entity>
</fetch>
WHERE (Mixing AND / OR conditions)
This example shows how to write FetchXML query to filter data using multiple conditions (Mixing AND / OR).
This FetchXML is the equivalent of below SQL
SELECT * FROM account
WHERE revenue > 0 AND
(
name LIKE 'A%' OR
name LIKE 'B%'
)
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<all-attributes />
<filter>
<condition attribute='revenue' operator='gt' value='0' />
<filter type='or'>
<condition attribute='name' operator='like' value='A%' />
<condition attribute='name' operator='like' value='B%' />
</filter>
</filter>
</entity>
</fetch>
SELECT DISTINCT (Exclude duplicates)
This example shows how to write FetchXML query to get distinct records (exclude duplicates).
This FetchXML is the equivalent of below SQL
SELECT DISTINCT accountid,name FROM account
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' distinct='true' version='1.0'>
<entity name='account'>
<attribute name='accountid' />
<attribute name='name' />
</entity>
</fetch>
SELECT TOP
This example shows how to write FetchXML query to select TOP N rows. This setting may not work if you using preview mode.
This FetchXML is the equivalent of below SQL
SELECT TOP 10 * FROM account
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' count='10' version='1.0'>
<entity name='account'>
<all-attributes />
</entity>
</fetch>
GROUP BY / Aggregate functions
This example shows how to use aggregate functions such as SUM, MIN, MAX, AVG along with GROUP BY clause. When you use Aggregate function in SQL Query you can also use alias for field. Refer FetchXML help to learn more about aggregate functions.
This FetchXML is the equivalent of below SQL
SELECT AVG(estimatedvalue) AS estimatedvalue_avg, COUNT(name) AS opportunity_count, ownerid AS ownerid
FROM opportunity
GROUP BY ownerid
You can use COUNT, COUNT(attribute_name), SUM, MIN, MAX, AVG as aggregate function
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='opportunity'>
<attribute name='estimatedvalue' aggregate='avg' alias='estimatedvalue_avg' />
<attribute name='name' aggregate='countcolumn' alias='opportunity_count' />
<attribute name='ownerid' alias='ownerid' groupby='true' />
</entity>
</fetch>
INNER JOIN (matching child records)
This example shows how to write FetchXML query to get account and associated contacts where email is @gmail.com or yahoo.com or hotmail.com and revenue for account is > 10000.
This FetchXML is the equivalent of below SQL
SELECT a.name, c.firstname, c.lastname, c.email
FROM account a
INNER JOIN contact c ON a.accountid=c.parentcustomerid
AND (c.email LIKE '%@yahoo.com'
OR c.email LIKE '%@gmail.com'
OR c.email LIKE '%@hotmail.com')
WHERE a.revenue > 10000
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='revenue' operator='gt' value='10000' />
</filter>
<link-entity name='contact' from='parentcustomerid' to='accountid' alias='c' link-type='inner'>
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='email' />
<filter type='or'>
<condition attribute='email' operator='like' value='%@yahoo.com' />
<filter type='or'>
<condition attribute='email' operator='like' value='%@gmail.com' />
<condition attribute='email' operator='like' value='%@hotmail.com' />
</filter>
</filter>
</link-entity>
</entity>
</fetch>
LEFT OUTER JOIN ('not in' type query)
This example shows how to write FetchXML query to find all leads that have no tasks, using an alias.
This FetchXML is the equivalent of below SQL
SELECT lead.FullName
FROM Leads as lead
LEFT OUTER JOIN Tasks as ab
ON (lead.leadId = ab.RegardingObjectId)
WHERE ab.RegardingObjectId is null
For more information on left outer join syntax check below link
https://msdn.microsoft.com/en-us/library/dn531006.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='lead'>
<attribute name='fullname' />
<link-entity name='task' from='regardingobjectid' to='leadid' alias='ab' link-type='outer'>
<attribute name='regardingobjectid' />
</link-entity>
<filter type='and'>
<condition entityname='ab' attribute='regardingobjectid' operator='null' />
</filter>
</entity>
<fetch/>
ORDER BY
This example shows how to write FetchXML query with order by specified for one or more attributes.
This FetchXML is the equivalent of below SQL
SELECT firstname, lastname, email FROM contact
ORDER BY lastname ASC, firstname DESC
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='contact'>
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='email' />
<order attribute='lastname' />
<order attribute='firstname' descending='true' />
</entity>
</fetch>
OPERATOR - Equal ( = )
This example shows how to write FetchXML query using equal operator.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE revenue=0
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='revenue' operator='eq' value='0' />
</filter>
</entity>
</fetch>
OPERATOR - Not Equal ( != )
This example shows how to write FetchXML query using not equal operator.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE revenue <> 0
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='revenue' operator='ne' value='0' />
</filter>
</entity>
</fetch>
OPERATOR - Greater Than ( > )
This example shows how to write FetchXML query using greater than operator.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE revenue > 0
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='revenue' operator='gt' value='0' />
</filter>
</entity>
</fetch>
OPERATOR - Less Than ( < )
This example shows how to write FetchXML query using greater than operator.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE revenue > 0
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='revenue' operator='gt' value='0' />
</filter>
</entity>
</fetch>
OPERATOR - IN (Check for multiple matching values)
This example shows how to write FetchXML query to find multiple matching values using IN operator.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE name IN ('Microsoft', 'Yahoo', 'Google')
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='name' operator='in'>
<value>Microsoft</value>
<value>Yahoo</value>
<value>Google</value>
</condition>
</filter>
</entity>
</fetch>
OPERATOR - NULL (Check for NULL values)
This example shows how to write FetchXML query to find NULL values (search for records where email not specified).
This FetchXML is the equivalent of below SQL
SELECT firstname, lastname, email FROM contact WHERE email is NULL
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='contact'>
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='email' />
<filter>
<condition attribute='email' operator='null' />
</filter>
</entity>
</fetch>
OPERATOR - NOT NULL (Check for NOT NULL values)
This example shows how to write FetchXML query to find NOT NULL values (search for records where email not specified).
This FetchXML is the equivalent of below SQL
SELECT firstname, lastname, email FROM contact WHERE email is NOT NULL
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='contact'>
<attribute name='firstname' />
<attribute name='lastname' />
<attribute name='email' />
<filter>
<condition attribute='email' operator='not-null' />
</filter>
</entity>
</fetch>
OPERATOR - LIKE (Pattern search)
This example shows how to write FetchXML query using LIKE operator to match string by pattern search.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE name LIKE 'zappy%'
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='name' operator='like' value='zappy%' />
</filter>
</entity>
</fetch>
OPERATOR - NOT LIKE (Pattern search)
This example shows how to write FetchXML query using LIKE operator to match string by pattern search.
This FetchXML is the equivalent of below SQL
SELECT name FROM account WHERE name NOT LIKE 'zappy%'
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch mapping='logical' version='1.0'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='name' operator='not-like' value='zappy%' />
</filter>
</entity>
</fetch>
OPERATOR - UNDER (Find all children for given hierarchy)
This example shows how to write FetchXML query to get children count for a given hierarchy.
For more information about querying hierarchical data check below link
https://msdn.microsoft.com/en-us/library/dn817893.aspx
For list of supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch distinct='false' no-lock='false' mapping='logical'>
<entity name='account'>
<attribute name='name' />
<attribute name='accountid' />
<attribute name='accountid' rowaggregate='CountChildren' alias='AccountChildren'/>
<filter type='and'>
<condition attribute='accountid' operator='under' value='{0}' />
</filter>
</entity>
</fetch>
Using DateTime Operators
This example shows how to write FetchXML query to get all tickets which are created 30 mins before.
Other similar operators are
olderthan-x-hours
olderthan-x-days
olderthan-x-weeks
olderthan-x-months
olderthan-x-years
For list of supported datetime operators
https://msdn.microsoft.com/en-us/library/gg334216.aspx
For list of all supported operators check below XSD and search for name="operator"
https://msdn.microsoft.com/en-us/library/gg309405.aspx
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch>
<entity name='incident'>
<attribute name='title' />
<attribute name='ticketnumber' />
<attribute name='createdon' />
<attribute name='incidentid' />
<filter type='and'>
<condition attribute='createdon' operator='olderthan-x-minutes' value='30' />
</filter>
</entity>
</fetch>
Query - All activities (sort, outer join, alias)
This example shows how to get all activities performed on lead or contact. It shows how to get attributes from lead and contact by using OUTER JOIN.
This FetchXML is the equivalent of below SQL
SELECT a.*
, l.firstname as lead_name,l.emailadddress1 as lead_email1
, c.firstname as contact_name
FROM activitypointer a
LEFT OUTER JONI lead l ON a.regardingobjectid=l.leadid
LEFT OUTER JONI lead c ON a.regardingobjectid=c.contactid
ORDER BY a.createdon DESC
To build custom FetxhXML
1. Login to your Dynamics CRM Portal
2. Click on Advanced Find icon (Found next to the search textbox)
3. Once done building query, Click on Download FetchXML icon. Copy that XML into Query editor of Dynamics CRM Source.
For more information about FetchXML query syntax check below links :
https://msdn.microsoft.com/en-us/library/gg328117.aspx
https://msdn.microsoft.com/en-us/library/gg328332.aspx
For complete syntax and validation rule for FetchXML check below XSD
https://msdn.microsoft.com/en-us/library/gg309405.aspx
<fetch version='1.0' mapping='logical'>
<entity name='activitypointer'>
<attribute name='subject' />
<attribute name='ownerid' />
<attribute name='regardingobjectid' />
<attribute name='activitytypecode' />
<attribute name='scheduledstart' />
<attribute name='scheduledend' />
<attribute name='instancetypecode' />
<attribute name='community' />
<attribute name='actualend' />
<attribute name='createdon' />
<link-entity name='lead' from='leadid' to='regardingobjectid' link-type='outer' alias='l'>
<attribute name='firstname' alias='lead_name' />
<attribute name='emailaddress1' alias='lead_email1' />
<attribute name='companyname' alias='lead_company' />
</link-entity>
<link-entity name='contact' from='contactid' to='regardingobjectid' link-type='outer' alias='c'>
<attribute name='firstname' alias='contact_name' />
</link-entity>
<order attribute='createdon' descending='true' />
</entity>
</fetch>