Back to articles
X++ Development

How to safely refresh a D365FO form datasource after you update data in X++

A practical guide to the difference between refresh(), reread(), research(), and executeQuery() on form datasources, and how to choose the right one after an X++ update.

How to safely refresh a D365FO form datasource after you update data in X++#

A common D365FO problem is this: your X++ code updates a record, but the form still shows old values, keeps stale filters, or suddenly jumps back to the top of the grid. The fix is usually not “just call refresh()”. In many cases you need a different datasource method depending on whether you changed the current record, need to reread the latest database state, or want to rerun the query altogether.

The important part is that these methods do different jobs. If you mix them up, the form may look wrong even though the update succeeded.

The four methods are not interchangeable#

On a form datasource, these methods are the ones you usually reach for:

  • refresh() repaints the datasource buffer on the form.
  • reread() reloads the current record from the database.
  • research() reruns the datasource query while preserving the current filters and sort order.
  • executeQuery() runs the datasource query again from scratch.

Microsoft’s datasource API documents reread() as reloading the current record from the database, research() as updating the database search defined by the current filters and sorts, and executeQuery() as the method you override or call to execute the datasource query again. (learn.microsoft.com)

When would you use each one?#

Use refresh() when the record is already correct, but the UI needs repainting#

If your code changed a field value on the current datasource buffer and you just need the form to display that updated buffer, refresh() is usually enough. It does not go back to SQL by itself; it redraws what the form already has.

This is why refresh() is often paired with reread() or a direct buffer change.

Use reread() when the database row may have changed#

If another process updated the same record, or your code changed it and you want the form to show the latest committed database values, use reread() first, then refresh(). Microsoft explicitly describes reread() as rereading the current record from the database. (learn.microsoft.com)

Use research() when you want the same query rules applied again#

research() is the method I normally use when the user should stay in the same filtered view, but the datasource needs to be re-evaluated after data changes. Microsoft says research() updates the database search defined by the filters and sorts currently applied to the form. (learn.microsoft.com)

Use executeQuery() when the query structure itself must be rebuilt#

If your extension changed ranges, joins, dynamic links, or other query logic, rerunning the datasource with executeQuery() is the safer choice. Microsoft documents executeQuery() as the datasource query execution method. (learn.microsoft.com)

A practical example#

Suppose you have a form datasource bound to a sales-related table, and a custom action updates the current record from another class or service.

A typical pattern looks like this:

X++
public void refreshCurrentRecord(FormDataSource _ds)
{
    if (!_ds)
    {
        return;
    }

    _ds.reread();
    _ds.refresh();
}

If the update changed the query result set, use research() instead:

X++
public void refreshFilteredList(FormDataSource _ds)
{
    if (!_ds)
    {
        return;
    }

    _ds.research();
}

If your code changed query ranges or you need to force the datasource to rebuild its query execution, call executeQuery():

X++
public void rerunDatasource(FormDataSource _ds)
{
    if (!_ds)
    {
        return;
    }

    _ds.executeQuery();
}

The reason this matters is that research() and executeQuery() do not behave the same way from the user’s point of view. research() is the closer fit when you want the current filter/sort context preserved, while executeQuery() is the better fit when the underlying query definition has changed. (learn.microsoft.com)

Common mistake: calling refresh() after an update and expecting the database to reload#

This is the most common trap. A form may still show stale data because refresh() only redraws the current buffer. If the record changed in the database, refresh() by itself does not fetch that new version.

That is why Microsoft’s datasource docs separate reread() from refresh(). reread() reloads the current record; refresh() repaints the form after the buffer is updated. (learn.microsoft.com)

Common mistake: using executeQuery() when you really want to preserve the user’s position#

If you call executeQuery() too aggressively, users can lose their current record position or see the grid reset more than necessary. In most cases, if you only need the current record updated, reread() plus refresh() is less disruptive.

Here is the rule of thumb I use in real projects:

  • Current record changed in SQL: reread() + refresh()
  • Datasource list should be rebuilt with current filters: research()
  • Datasource query itself changed: executeQuery()
  • Only the on-screen buffer changed: refresh()

That keeps the UI behavior predictable and avoids the “why didn’t my form update?” debugging loop.

Things to watch for#

Don’t refresh before the transaction is finished#

If your update happens inside a transaction, make sure the data is actually committed before forcing the UI to reread the record. X++ transactional integrity depends on ttsBegin, ttsCommit, and ttsAbort, and Microsoft notes that the transaction scope matters for update consistency. (learn.microsoft.com)

Be careful with datasource extensions#

If you are extending executeQuery() or another datasource method, make sure your refresh logic does not trigger unwanted recursion. The method you choose should match the kind of change you made to the datasource, not just the symptom you are trying to hide. The datasource APIs support overriding these methods directly on the form datasource. (learn.microsoft.com)

Conclusion#

If a D365FO form shows stale data after X++ updates, the fix is usually to choose the right datasource method, not to add more refresh calls. Use reread() when you need the latest row, research() when the filtered list should be recalculated, executeQuery() when the query itself changed, and refresh() when the buffer is already correct but the form needs repainting.

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