How do I refresh a form datasource after updating a record in X++?
I’m updating a record in X++ outside of the form datasource. The update is saved to the database, but the form continues to show the old value until I manually refresh it.
What is the correct way to refresh the datasource after the update? I’ve seen refresh(), reread(), research(), and executeQuery() used for this, but I’m not clear on when each one should be used.
1 answer
Write an answerThe short answer is, it depends on what you're trying to refresh.
If something updated the current record and you just need the form to show the new values, I normally use:
MyTable_ds.reread();
MyTable_ds.refresh();reread() grabs the latest version of the current record from the database.
refresh() updates the controls on the form with what's currently in the datasource buffer.
So together you're basically saying:
Go get the latest version of this record and show it on the form.
What about research()?#
If I need to refresh the actual list of records, I'll use:
MyTable_ds.research();This reruns the datasource query.
For example, if my code added or deleted records, just calling refresh() isn't necessarily enough. The datasource needs to run the query again to figure out which records should be displayed.
Most of the time I'll actually use:
MyTable_ds.research(true);The true tells D365FO to try to keep the current record selected.
That's useful on list pages or forms where you don't want the user to lose their position every time you refresh the datasource.
What about executeQuery()?#
You'll also see this:
MyTable_ds.executeQuery();I normally use executeQuery() when I've actually changed something about the query.
For example, if I add a range:
QueryBuildDataSource qbds = MyTable_ds.queryBuildDataSource();
SysQuery::findOrCreateRange(
qbds,
fieldNum(MyTable, Status)).value(
queryValue(MyStatus::Open));
MyTable_ds.executeQuery();I changed the query, so now I want the datasource to execute it again.
Easy way to remember it#
| Method | When I'd use it |
|---|---|
refresh() | Redraw what's currently in the datasource buffer |
reread() | Get the latest version of the current record from the database |
research() | Rerun the datasource query |
research(true) | Rerun the query and try to stay on the same record |
executeQuery() | I changed the query or its ranges and need to execute it again |
So if you updated the current record somewhere else and the form is still showing the old value, start with:
MyTable_ds.reread();
MyTable_ds.refresh();If records were added/deleted or you need to refresh the whole list:
MyTable_ds.research(true);And if you changed the actual query:
MyTable_ds.executeQuery();You definitely don't need to call all of them just because the form isn't refreshing. Figure out what changed and refresh only what you actually need.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer