Why does a QueryBuildDataSource range added in executeQuery/active not filter the form grid query?
I’m extending a form datasource and I add a range to a QueryBuildDataSource inside my override of the executeQuery/active/refresh pattern (i.e., before the datasource populates the grid). The code successfully modifies the Query/QueryBuildDataSource object I’m touching, but the grid still shows records as if the range wasn’t applied.
What’s the correct place/pattern to add a QueryBuildDataSource range in a form datasource override so it isn’t lost when the base datasource rebuilds/recreates the query later in the lifecycle?
1 answer
Write an answerWhy your range “sticks” to the QueryBuildDataSource but doesn’t filter the grid#
In a form datasource, adding a range during executeQuery()/active()/refresh() is often too early (or just on the wrong instance) because the form framework will later rebuild the datasource query (create/clone a new Query and re-create its QueryBuildDataSource tree) when:
- the base datasource’s
executeQuery()runs the standard logic, - the datasource performs its query refresh/rebuild during lifecycle,
- the grid datasource’s query is rebuilt from the form’s query definitions / user provided values / SysQuery patterns.
So your code may mutate the QueryBuildDataSource of one Query instance, but then the datasource replaces that query with a new one; the grid ends up showing results from the rebuilt query (without your range).
Correct pattern: override executeQuery() and add the range after the query is ensured (and only on the right datasource instance)#
The most reliable hook is:
- Call
next executeQuery()so the framework rebuilds/initializes the datasource query. - Then add your
QueryBuildRangeto the datasource’s currentQueryBuildDataSource(the one that will actually be executed).
Typical shape:
[DataSource]
class MyFormDataSource extends FormDataSource
{
public void executeQuery()
{
next executeQuery();
// IMPORTANT: use this.query() / this.queryBuildDataSource() *after* next
// and target the correct QBDS (including links/child data sources if needed)
Query q = this.query();
QueryBuildDataSource qbds;
QueryBuildRange qbr;
// Example: locate the data source by name (adapt to your form)
qbds = q.dataSourceTable(tableNum(MyTable));
if (!qbds)
{
return;
}
qbr = qbds.findRange(fieldNum(MyTable, MyField));
if (!qbr)
{
qbr = qbds.addRange(fieldNum(MyTable, MyField));
}
qbr.value(strFmt('%1', someValue()));
}
}Why this works#
next executeQuery()makes sure the form datasource has finished whatever query rebuild/recreation it needs.- After that,
this.query()returns the currentQueryobject that will be used for population. - Adding/updating the range on that post-
nexttree guarantees the grid query uses the range.
Don’t do it in active()/refresh() (unless you re-apply after the rebuild)#
active()is about UI state, not “final query assembly”.refresh()frequently triggers datasource query execution, and it may occur in multiple places; you’ll still race against (or be overwritten by) the datasource’s rebuild logic.
If you must react to control changes (e.g., a field on the form), do:
- store your filter value in a variable,
- call
this_ds.executeQuery()(orresearch) and apply the range inexecuteQuery()as shown.
Key edge cases / gotchas#
- Wrong QBDS instance: Mutating a QBDS reference from an earlier query instance won’t affect the later rebuild. That’s the most common cause.
- Ranges get duplicated:
addRange()repeatedly can create multiple ranges depending on your logic. PreferfindRange()and thenvalue(). - Need to add range to child/linked QBDS: If your filter belongs to a joined/child datasource, you must locate the correct QBDS in the
Query(not the root). - Record selection vs display: If you instead used display methods/
selectionChanged, you could affect what looks selected but not what the grid retrieved. - Cross-company: If your filter is company-dependent, set it consistently (or make sure your filter respects
CompanyId/cross-company settings). The query rebuild can change how data is scoped.
Bottom line#
Put the range addition in executeQuery() after next executeQuery(), targeting the datasource’s current QueryBuildDataSource and updating the range via findRange()/value().
That ensures your range isn’t lost when the framework recreates the query later in the datasource lifecycle.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer