Why `executeQuery()` wipes user filters on a D365FO form datasource
`executeQuery()` is the right call when you changed a form datasource query in code, but it also rebuilds the datasource query run and can drop user-applied filters. If you only need to re-read data while preserving filters and sort order, `research()` is usually the safer choice.
Why executeQuery() wipes user filters on a D365FO form datasource#
If your form suddenly loses the user's filters after you call executeQuery(), that is usually expected behavior in D365FO, not a bug in your code. research() reruns the current form query and keeps the user’s applied filters and sorting, while executeQuery() reruns the datasource query as the form currently defines it and is meant for cases where you changed the query itself. Microsoft’s API docs also show refresh() only redraws the view, reread() reloads the current record, and executeQuery() is what the form uses when opening a datasource for data display. (learn.microsoft.com)
Quick answer#
Use this rule of thumb:
refresh(): repaint what is already in the form cache.reread(): reload the current record from the database.research(): rerun the existing datasource query and keep user filters/sorts.executeQuery(): rerun the datasource query when you have changed the query in code. (learn.microsoft.com)
That is why executeQuery() is often the wrong choice if your only goal is “show the latest data on screen.” Community examples and Microsoft’s API remarks both point to the same distinction: research() is for refreshing results without discarding the current form query state, while executeQuery() is for a query reset/rebuild scenario. (community.dynamics.com)
Why this happens#
A D365FO form datasource has more than one layer of query state. The practical difference is that the user can apply filters and sorts to the live form query, and research() is designed to work with that current query state. executeQuery(), on the other hand, is tied to the datasource query definition itself and is what the framework uses when the form opens or when the query needs to be re-executed from the datasource definition. Microsoft’s QueryRun and datasource documentation support that model. (learn.microsoft.com)
That is also why people notice a difference after clicking refresh actions, opening a form, or invoking code from buttons or event handlers. If the code path rebuilds the datasource query, the user’s ad hoc filters may disappear. If the code path only needs fresh rows from the existing query, research() is the better fit. (community.dynamics.com)
Practical fix#
If your code does not change the datasource query, prefer research(true) or research() instead of executeQuery().
public void refreshSalesTable(FormRun _formRun)
{
FormDataSource salesTableDs = _formRun.dataSource(formDataSourceStr(SalesTable, SalesTable));
// Reruns the current query and keeps user filters/sorts.
salesTableDs.research(true);
salesTableDs.refresh();
}If you only changed values on the current record and want the screen to repaint, use reread() followed by refresh():
public void rereadCurrentRecord(FormRun _formRun)
{
FormDataSource salesTableDs = _formRun.dataSource(formDataSourceStr(SalesTable, SalesTable));
salesTableDs.reread();
salesTableDs.refresh();
}Use executeQuery() only when you intentionally changed the datasource query and want the form to reflect that new query definition. Microsoft’s examples and remarks show it as the method to use when the form query itself is being re-executed. (learn.microsoft.com)
Common mistake#
A very common pattern is calling both reread() and research() or refresh() and executeQuery() in the wrong combination. That usually makes the code harder to reason about and can still produce confusing UI behavior. The cleaner approach is to choose the method that matches the actual need:
- changed only the current record →
reread()+refresh() - changed data underneath the same query →
research(true)+refresh() - changed the form query itself →
executeQuery()(community.dynamics.com)
When executeQuery() is the right choice#
Use executeQuery() when your extension or override actually modifies the datasource query. Typical cases include:
- adding or removing ranges
- changing join behavior
- switching which records should be returned
- rebuilding the datasource based on form logic or user input
In those cases, calling research() would refresh the old query state, which is not what you want. (learn.microsoft.com)
If you need to preserve the user's position#
research(true) is useful when you want the datasource to refresh and still keep the cursor close to the current row. Community guidance on this method repeatedly calls out the retain-position behavior, and that matches how practitioners usually use it in real forms. (community.dynamics.com)
Recommended pattern#
In most form code, I would use this decision tree:
- Did the code change the query? Use
executeQuery(). - Did the code only change data? Use
reread()andrefresh(). - Do you just need to rerun the current query and keep filters? Use
research(true)and thenrefresh()if needed. (learn.microsoft.com)
That is the safest way to avoid the classic “why did my filters disappear?” problem.
Bottom line#
If your form is losing user filters, the fix is usually not “refresh harder.” It is usually “use the right datasource method.” For most post-update scenarios, research() is the better default. Save executeQuery() for cases where you intentionally changed the query itself. (learn.microsoft.com)
Sources#
- FormDataSource.refresh Method (Dynamics.AX.Application) — Microsoft Learn
- FormDataSource.executeQuery Method (Dynamics.AX.Application) — Microsoft Learn
- FormDataSource.reread Method (Dynamics.AX.Application) — Microsoft Learn
- FormDataSource.research Method (Dynamics.AX.Application) — Microsoft Learn
- FormDataSource.queryRun Method (Dynamics.AX.Application) — Microsoft Learn
- QueryRun Class (Dynamics.AX.Application) — Microsoft Learn
- Preserve form query filters after explicit executeQuery call on a data source (Helper class included) — Dynamics 365 Community Blogs
Missing something?
Suggest a topic you'd like to see covered next. The most requested topics help shape future XppForge documentation.
Sign in to suggest a topic