Microsoft Dataverse limits the maximum number of rows that can be retrieved in a single query to 5000. But often while working with large datasets, there occurs a need to fetch more than 5000 rows and this blog will discuss how it can be achieved in Power Automate.
List rows action in Power Automate is used to retrieve multiple rows at once from Dataverse with a structured query – ‘Filter rows’ or ‘Fetch Xml Query’. Filter rows is an OData-style filter expression used to narrow down the set of rows that Dataverse returns whereas Fetch Xml Query is a Dataverse-style FetchXML query which allows more flexibility in building custom queries especially while working with a table that has multiple related tables.
While using Filter rows to retrieve more than 5000 rows, ‘Pagination‘ feature must be enabled in the Settings and the maximum number of requested rows must be entered in ‘Threshold‘.
Note: The maximum configurable threshold is 100,000.
Enabling this feature helps to retrieve rows up to the specified threshold in a single query as shown below,
Quick tip: While paging large result sets, it is best to process the workload considering the content throughput limits and message size limits that Power Automate applies to ensure general service guarantees.
Fetch Xml Query:
Unfortunately, the Pagination feature does not work when Fetch Xml Query is used to retrieve more than the default limit of 5000 rows. In this case, ‘page’ attribute can be used in the FetchXML to page through and retrieve a large dataset. The below is a simple approach to achieve this with minimal actions.
Step 1: Initialize two variables – MoreRecords and Total Rows Count (Optional).
MoreRecords – A string variable to store the value of “@Microsoft.Dynamics.CRM.morerecords”
(While querying a set of rows, if there are more rows available in the upcoming pages, the value of “@Microsoft.Dynamics.CRM.morerecords” in the body of the result set will be true. If there are no further records available, “@Microsoft.Dynamics.CRM.morerecords” will not be available in the result set.).
Total Rows Count (Optional) – An integer variable to store the count of the total rows retrieved.
Step 2: Add a Do until control to retrieve the rows until MoreRecords variable is empty i.e., until there are no more rows available to be retrieved.
Step 3: Add List rows action inside the Do until to retrieve the targeted rows by specifying the FetchXML. In the query, in
Step 4: Inside the Do until after the List rows action, set MoreRecords with the value of “@Microsoft.Dynamics.CRM.morerecords” from the body of List rows result.
Follow it by incrementing the Total Rows Count with the count of rows retrieved.
Finally compose Total Rows Count outside the Do until to see the aggregated count of total rows retrieved.
Below is the result of running this sample flow to page results of Fetch Xml Query,
Note: Ensure that the Pagination feature is disabled while using Fetch Xml Query to retrieve multiple pages of large result sets. Else, only up to 5000 rows (default limit) will be fetched and no information regarding the next page of rows will be available in the retrieved result set.