Back to articles
X++ Development

Why a D365FO form filter disappears after `executeQuery()` in a datasource extension

If you add or rebuild a form datasource query in X++ and the user’s filter suddenly disappears, the problem is usually that `executeQuery()` reran the datasource query instead of preserving the current search state. This article shows when that happens, why it happens, and how to keep user filters intact without breaking the form.

Why a D365FO form filter disappears after executeQuery() in a datasource extension#

If your form extension adds a range, rebuilds the datasource query, and then the grid suddenly loses user-entered filters, the root cause is usually simple: executeQuery() runs the datasource query again, while research() is the method designed to rerun the search with the current filters and sorts still applied. refresh() only redraws what is already loaded, and reread() reloads the current record from the database. (learn.microsoft.com)

This is one of those D365FO behaviors that looks inconsistent until you separate requerying data from preserving the current search context.

Quick answer#

Use this rule of thumb:

  • refresh(): repaint the form from the data already in memory.
  • reread(): reload the current record from the database.
  • research(): rerun the search while keeping the current filters and sorts.
  • executeQuery(): execute the datasource query again from the form query definition. (learn.microsoft.com)

If your problem is “the grid is stale but I want the user’s filters to stay in place,” research() is usually the right fix. If you rebuild the query itself, executeQuery() may still be necessary, but you need to be deliberate about when you call it. Microsoft’s own documentation warns that changing a form query late in the form lifecycle can interfere with saved views and query behavior. (learn.microsoft.com)

Why this happens#

executeQuery() is not a soft refresh. It reruns the datasource query. In contrast, research() is documented as updating the database search defined by the current filters and sorts already applied to the form. That distinction is the key reason user filters appear to “disappear” after an extension calls executeQuery() and rebuilds ranges or query conditions. (learn.microsoft.com)

Microsoft also notes that query modifications made after super() in executeQuery() can affect how the query behaves with saved views and personalization. In practice, that means a form extension that changes the datasource query at the wrong point can override or reset the user’s current view of the data. (learn.microsoft.com)

The safe pattern#

If you only need to show updated data after a record change, prefer this sequence:

X++
public void write()
{
    super();

    myDataSource_ds.reread();
    myDataSource_ds.refresh();
}

That pattern updates the current record and redraws the datasource without rebuilding the search context. Microsoft’s documentation uses the same general pattern when combining reread() and refresh() after a write operation. (learn.microsoft.com)

If you need to rerun the existing query and keep filters:

X++
public void clicked()
{
    super();

    myDataSource_ds.research();
}

If you truly need to rebuild the query definition, do that intentionally and only then call executeQuery():

X++
public void executeQuery()
{
    QueryBuildDataSource qbds = this.query().dataSourceTable(tableNum(MyTable));

    // Add or adjust ranges before calling next.
    // Example only: keep the logic small and explicit.

    next executeQuery();
}

The important part is that this is a query rebuild, not a visual refresh. (learn.microsoft.com)

When executeQuery() is the wrong choice#

A lot of form extensions use executeQuery() because it seems like the “refresh” method. That works until users apply filters, saved views, or sorts they expect to keep. Because executeQuery() reruns the datasource query, it can discard the current search experience that research() preserves. (learn.microsoft.com)

So if your code does something like this:

X++
public void executeQuery()
{
    next executeQuery();

    // Bad idea if you want to preserve current search state.
    myDataSource_ds.executeQuery();
}

you are very likely forcing the grid back to the base query behavior and losing the active filter context.

Practical decision tree#

Use this instead of guessing:

  1. Did only the current record change?
    • Use reread() and refresh().
  2. Did the underlying rows change, but you want to keep current filters?
    • Use research().
  3. Did the query definition itself change?
    • Use executeQuery(), but expect the search context to be rebuilt.
  4. Did you put query logic in executeQuery() and now saved views act strangely?
    • Move the logic earlier in the form lifecycle or reassess whether the query really needs to be rebuilt there. (learn.microsoft.com)

Common mistake#

The most common mistake is calling executeQuery() because the data changed, when the real need is only to redraw or reread.

That mistake causes three problems:

  • the form re-executes more aggressively than needed,
  • user filters can disappear,
  • saved views and personalization can behave differently than expected. (learn.microsoft.com)

A good extension strategy#

In form extensions, keep query changes small and predictable:

  • add or remove ranges only when you actually need to change the query result set,
  • use research() when the user should keep the current search context,
  • use refresh() for UI repainting,
  • use reread() when you need current database values for the active record. (learn.microsoft.com)

That separation usually fixes the “my filter vanished” complaint without extra hacks.

Bottom line#

If a D365FO form filter disappears after your X++ code runs, you probably called the wrong datasource method.

  • refresh() redraws.
  • reread() reloads the current row.
  • research() reruns the search and keeps the user’s filters.
  • executeQuery() rebuilds the query result and can reset the current search experience. (learn.microsoft.com)

If you remember only one thing: use research() when you want the grid to update without throwing away the user’s filter context.

Sources#

Was this article helpful?Sign in to vote

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