D365FO form datasource extension: safe CoC for modified/active/executeQuery without recursion
In Dynamics 365 Finance and Operations (D365FO), I’m extending a form datasource using Chain of Command (CoC).
I need to add logic around these methods on the datasource:
modified()active()executeQuery()(or a refresh method that ends up callingexecuteQuery)
What’s the recommended pattern to:
- Call the next method exactly once (
next modified()/next active()/next executeQuery()), - Avoid accidental recursion when the added logic triggers a refresh/research that re-enters the same override/CoC chain,
- Decide whether the logic should go in the datasource CoC, a control event handler (e.g.,
modifiedon a control), or in a helper method called from the CoC layer.
Assume I’m extending an existing standard datasource and want to preserve default behavior while filtering/refreshing data based on current field values. How should the implementation be structured (including common guards/flags or state checks) to prevent re-entry loops and double execution?
1 answer
Write an answerRecommended pattern: “single next call + re-entrancy guard” in datasource CoC#
When extending a form datasource method with CoC (modified/active/executeQuery), the safest approach is:
- Call
next ...exactly once. - Add a re-entrancy flag (and optionally compare
args/state) so that if your logic triggers a refresh that re-enters the same override, the second entry becomes a no-op. - Keep the CoC method thin: do only state bookkeeping + guard + call next once, then call a helper that performs filtering/refresh.
This avoids recursion and double execution while preserving the original datasource behavior.
1) Datasource CoC implementation skeleton#
Use one flag per lifecycle method (or a shared “refresh-in-progress” flag). The key is: set flag before triggering anything that can lead back into the same CoC method, and clear it in
finally.
// Example CoC on your datasource class
[ExtensionOf(classStr(YourBaseFormDataSource))]
final class YourFormDataSource_Extension
{
boolean m_executeQueryGuard;
boolean m_modifiedGuard;
boolean m_activeGuard;
// Helper entry point
private void applyFilterAndMaybeRefresh()
{
// Put your filtering/refresh logic here.
// IMPORTANT: If this helper causes executeQuery(), only call it when guards permit.
// Example: call a datasource query refresh, or set ranges, etc.
}
public boolean modified()
{
// Always call next exactly once
boolean ret = next modified();
// Guard re-entry (covers cases where modified->refresh->modified)
if (m_modifiedGuard)
{
return ret;
}
m_modifiedGuard = true;
try
{
// Decide if this modification should trigger refresh/filtering.
// Common checks:
// - Are we running in a state where the datasource query is available?
// - Has relevant field value actually changed?
applyFilterAndMaybeRefresh();
}
finally
{
m_modifiedGuard = false;
}
return ret;
}
public boolean active()
{
boolean ret = next active();
if (m_activeGuard)
{
return ret;
}
m_activeGuard = true;
try
{
// Usually you should only do lightweight work in active().
// If you must refresh, use the executeQuery guard inside executeQuery override.
// e.g. set up state used by executeQuery.
}
finally
{
m_activeGuard = false;
}
return ret;
}
public void executeQuery()
{
// Guard recursion. executeQuery is a common entry point.
if (m_executeQueryGuard)
{
next executeQuery();
return;
}
m_executeQueryGuard = true;
try
{
// Call next exactly once.
next executeQuery();
// Apply filtering AFTER next, if your intent is to affect what the base builds.
// Alternatively, if the base relies on your ranges being set BEFORE it runs,
// then set ranges BEFORE calling next.
// Pick one approach consistently.
// Example shape (pseudo):
// QueryBuildDataSource qbds = this.queryBuildDataSource();
// qbds.addRange(fieldNum(YourTable, YourField)).value(<expr>);
// If you call refresh from here, DO NOT—because you'd loop.
// Instead, set query ranges here and let next execute the query.
}
finally
{
m_executeQueryGuard = false;
}
}
}Why this works#
next modified()/next active()/next executeQuery()is called exactly once.- Guards prevent cycles like:
modified()triggers refresh ⇒executeQuery()⇒ yourexecuteQuery()logic triggers another refresh ⇒ back intoexecuteQuery().
finallyensures flags are cleared even if errors occur.
2) Where to put the logic: datasource CoC vs control event handler#
Use control event handler when possible (recommended)#
- If the refresh is caused by a specific user changing a field (control) on the form, prefer overriding/extension of the control event (e.g.,
modifiedon the control) and call a targeted datasource method. - This reduces the number of times your logic runs (datasource
modified()can fire for many reasons).
Pattern:
- Control
modifiedhandler sets datasource state (or ranges) and then calls one refresh path (ideally justresearch/reloadfor the datasource). - The datasource
executeQuery()applies the actual filtering based on that state.
This leads to one-way dependency:
- Control changes ⇒ update state ⇒ datasource
executeQuery()applies query
Use datasource executeQuery CoC for query filtering#
- If your goal is “filtering data based on current field values”, the most stable place is usually
executeQuery():- It runs right before the query executes.
- It’s a single choke point for ranges.
Use datasource modified()/active() only for state transitions#
- These can be noisy and are easier to accidentally re-enter.
- If you still use them:
- Keep them lightweight.
- Don’t call refresh methods that will re-enter
executeQuery()from insideexecuteQuery().
3) Guarding against double execution: check “relevant change”#
Even with re-entrancy guards, you can still cause redundant refreshes (performance issue). Add an additional check:
- Cache the last values used for filtering.
- Only refresh when the filter-driving values changed.
Example approach (conceptual):
- Store
m_lastCustomer,m_lastStatus, etc. - In
applyFilterAndMaybeRefresh(), compare current values vs cached. - If unchanged, do nothing.
This prevents:
- Multiple
modified()events during data binding / UI changes. - Refresh happening during
active()when values were not modified.
4) Critical edge cases#
A) Guarding executeQuery() is usually the most important#
If your filtering ultimately depends on changing ranges, you should set ranges in executeQuery() and avoid triggering executeQuery from inside executeQuery.
B) modified() may be called during form initialization / record navigation#
- Don’t assume “user changed it” vs “system set it”.
- If your logic is only relevant for user changes or after initialization, add state checks (e.g., “initialized” flag).
C) Multiple datasources / nested refresh#
If your filtering touches relations or other datasources, refresh cascades can happen. Keep refresh to one datasource per change and be aware that other datasources may call your datasource indirectly.
D) Ordering relative to next executeQuery()#
- If you add ranges after
next executeQuery(), they may be applied too late (base query already executed). - If you add ranges before
next executeQuery(), they influence the query that base runs.
So choose one:
- Preferred: set ranges before
next executeQuery()if you’re changing the query the base executes. - If you must adjust after, ensure you don’t rely on the query already having run.
5) Practical “best-effort” structure to follow#
Recommended architecture (robust and low-recursion risk):
- Control modified handler:
- updates a small set of datasource “filter state” variables.
- triggers a single refresh (research/reload) on the datasource.
- Datasource executeQuery CoC:
- reads filter state variables
- sets query ranges
- calls
next executeQuery()exactly once (usually after ranges are set). - no further refresh calls.
- Datasource modified/active:
- only for exceptional state transitions; use re-entrancy guards.
Quick rule of thumb#
- If you need to prevent recursion: guard + never call refresh from the same method that is re-entered indirectly.
- If you need filtering: put it in
executeQuery()and let it be the only place that mutates the query right before execution.
If you share which exact refresh method you were calling (e.g., research, refresh, executeQuery, or calling another datasource’s methods) and whether you apply query ranges before/after next executeQuery(), I can tailor the guard placement and ordering more precisely.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer