Refresh, reread, research, executeQuery – which one to use?

 Even if they are x++ senior development consultants, many people are still confused about these four data source methods. Therefore, the author wrote this blog to clarify the similarities and differences of these four data source methods and demonstrate the common application scenarios of each method. The authors have sorted the methods according to their impact on row records in the grid.

Common mistakes

Typically, developers call 2 of the above methods in the following order:

formDataSource.refresh()
formDataSource.research()

or
formDataSource.reread()
formDataSource.research()

or
formDataSource.research()
formDataSource.executeQuery()

or
formDataSource.research()
formDataSource.refresh() / formDataSource.reread()

All of these practices are wrong, or at least partially redundant. Hopefully after reading the full post, you won't have any misconceptions about these 4 methods.

Refresh

This method only refreshes the data displayed in the grid control using the data source content in the cache. Calling the refresh() method will not re-read the records in the database. Therefore, if another process has updated database records, doing refresh() will not show those changes.

refreshEx

Determines how grid rows are redrawn based on an optional parameter value.

-1: Update all records

-2 (default value): update all checked records and records that specify the displayOptions option

Also note that refreshEx() only redraws fields that are in the grid, so controls that are not in the grid may still contain old values. But the Refresh() method updates all values.

Reread

The reread() method will query the database and re-read the current record content into the data source cache. But the content displayed on the grid will not be updated until the grid contents are redrawn (for example, when you navigate away from the row or reopen the form).
This method should not be used to refresh grid data if records have been added or deleted via code. For this, you can utilize other methods described below

The reread() and refresh() methods are often used in combination

Typically, when you change some value in the current record through some code (for example, when the user clicks a button), and update the database by calling the update method on the table buffer, you may want to show the user that the change occurred up. In this case, call the reread() method to update the data source form cache with the values ​​from the database (this does not update the screen), then call refresh() to redraw the grid and display the changes to the user.

Button SaveRecord property == Yes

Each button has a SaveRecord property, which defaults to Yes. Whenever the button is clicked, the changes made in the current record will be saved to the database. So calling reread() doesn't restore the original record value as some might expect. If the user expects this, this property should be set to No.

research

Calling research() will keep the existing query conditions on the form, re-read the values ​​from the database, and keep the filtering, sorting and other conditions set by the user on the form.

Research(true)

Starting from AX 2009, the research() method adds an optional Boolean parameter _retainPosition. If research(true) is called, the cursor will stay on the original record of the grid after refreshing the data, this is a very useful enhancement that solves the problem of cursor reset (the findRecord method is an alternative, but has poor performance ).

ExecuteQuery

Calling executeQuery() will also requery the records from the database and update the rows in the grid. The difference from the research() method is that if you need to change the query conditions in the code and re-execute the query with the new query conditions, you can use the ExecuteQuery() method.

formDataSource.queryRun().query() vs formDataSource.query()

What needs to be reminded is that the form has two object instances about queries: one is the original data source query (stored in formDataSource.query()); the other is the current user data source query, which contains the filter conditions added by the user (stored in formDataSource.queryRun().query()). When the research() method is called, the system kernel creates a new instance of queryRun using formDataSource.queryRun().query() as the base. Therefore, the filter conditions set by the user on the data source will be preserved. This is useful, for example, when multiple users are using the same form, so each user can display data according to their own set of filters.

In addition, calling executeQuery() will use the default query conditions defined by the form to query, and will remove the filter conditions set by the user.
This is something everyone should fully understand when using the research/executeQuery method to prevent conflicts with user-set filter conditions when updating the query.

Comments