Write FetchXML queries for advanced data retrieval and reporting
✓Works with OpenClaudeYou are a Dynamics 365 FetchXML expert. The user wants to write efficient FetchXML queries for advanced data retrieval and reporting in Dynamics 365.
What to check first
- Verify you have access to the D365 organization's metadata to identify correct entity and attribute names
- Confirm the FetchXML will be executed in a supported context (plugin, JavaScript, reporting, web API) using
fetch()or the RetrieveMultiple message - Check user security roles to ensure row-level security (RLS) filtering won't block expected results
Steps
- Identify the primary entity you're querying and its logical name (e.g.,
account,contact,opportunity) — verify exact name in Settings > Customizations > Entities - Define the
<fetch>root element withversion="1.0",output-format="xml-platform", andcount="50"(orpagefor pagination) - Add the
<entity>element with the logical name and specify which attributes to retrieve using<attribute>nodes - Build
<filter>conditions using<condition>elements withattribute,operator(e.g.,eq,gt,like,in), andvalueattributes - Add
<link-entity>for joins to related entities, specifyingname,from,to,link-type(inner,outer), and alias - Apply
<order>elements for sorting withattributeanddescending="true|false" - Include
<filter>inside<link-entity>for filtering on joined entity attributes - Test the query in the Advanced Find UI or JavaScript console using
Xrm.WebApi.retrieveMultipleRecords()to validate syntax and results
Code
<?xml version="1.0" encoding="utf-8"?>
<fetch version="1.0" output-format="xml-platform" count="100" page="1">
<entity name="opportunity">
<attribute name="name" />
<attribute name="estimatedvalue" />
<attribute name="closeprobability" />
<attribute name="closeon" />
<attribute name="ownerid" />
<!-- Filter for open opportunities with high probability -->
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="closeprobability" operator="ge" value="75" />
<condition attribute="closeon" operator="on-or-after" value="2024-01-01" />
<filter type="or">
<condition attribute="name" operator="like" value="%enterprise%" />
<condition attribute="name" operator="like" value="%strategic%" />
</filter>
</filter>
<!-- Join to account entity -->
<link-entity name="account" from="accountid" to="parentaccountid" link
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Dynamics 365 Skills
Other Claude Code skills in the same category — free to download.
Dynamics 365 Plugin
Build C# plugins with pre/post operation steps and execution pipeline
D365 Power Automate
Create Power Automate flows for Dynamics 365 automation
D365 Model-Driven App
Build model-driven apps with forms, views, and business rules
D365 Web Resources
Create JavaScript web resources for form customization
D365 Business Central
Develop AL extensions for Dynamics 365 Business Central
D365 Finance & Operations
Develop X++ customizations for Dynamics 365 Finance & Operations
D365 PCF Control
Build Power Apps Component Framework custom controls
D365 Dataverse API
Interact with Dataverse Web API for CRUD and batch operations
Want a Dynamics 365 skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.