D365FO Chain of Command: when to use class extensions vs event handlers
A practical guide to choosing between Chain of Command and event handlers in Dynamics 365 Finance and Operations, with the tradeoffs that matter in real development work. It explains why extensibility points exist, where method wrapping fits, and how to avoid the common mistake of picking the wrong extension pattern.
D365FO Chain of Command: when to use class extensions vs event handlers#
If you’ve worked in D365 Finance and Operations for more than a few projects, you’ve probably seen code that could have been written with a Chain of Command extension but ended up as an event handler instead — or the other way around.
That choice matters. It affects upgradeability, readability, reuse, and how easy the code is to debug later.
The short version is:
- use Chain of Command when you need to wrap an existing method and influence its behavior before or after the original logic
- use event handlers when the artifact exposes events and you want to react to a lifecycle point without changing the method flow
- don’t use either one just because they are available; use the extension point that matches the problem
Microsoft’s extensibility model is built around extensions rather than overlayering, and protected/public methods are intended to be extension points. Microsoft also recommends writing small, focused methods so they can be extended cleanly. (learn.microsoft.com)
When would you use Chain of Command?#
Use Chain of Command when you need to alter the behavior of an existing class, table, or form method and the method is designed for extension.
Typical examples:
- add extra validation before a standard method runs
- change a return value after the original logic finishes
- supplement standard behavior without copying the whole method
- call
nextand keep the base logic in place
This is the normal fit for class extensions in D365FO. Microsoft’s guidance on class extensions describes method wrapping and Chain of Command as the controlled way to customize behavior without touching the original source. (learn.microsoft.com)
When would you use event handlers?#
Use event handlers when the artifact exposes an event and you want to react to that event without wrapping the original method.
This is often a better fit when:
- the platform already raises a relevant data event
- you only need to react to a state change
- you don’t want your logic to depend on the internal flow of the original method
- multiple extensions may need to subscribe independently
For extensible tables, Microsoft explicitly recommends using the normal table methods such as insert(), update(), and delete() because they raise data events, while doInsert(), doUpdate(), and doDelete() bypass those events and make the table harder to extend. (learn.microsoft.com)
The practical difference#
This is where many implementations go wrong.
A Chain of Command method is part of the execution path. You are wrapping logic.
An event handler is not part of the main method flow. You are responding to a published event.
That means these two patterns solve different problems:
- CoC changes or augments behavior in a specific method
- event handlers listen for lifecycle moments that the application exposes
If you need to influence a method’s return value, skip a step, or conditionally change how the method proceeds, CoC is usually the right tool. If you just need to react after a table write or on a form event, an event handler is often cleaner.
Example: CoC for a method extension#
A simple CoC extension looks like this:
[ExtensionOf(classStr(SalesFormLetter_Invoice))]
final class MySalesFormLetter_Invoice_Extension
{
public void update()
{
// Pre-processing here
next update();
// Post-processing here
}
}The important part is the next call. That keeps the original implementation in the chain. If you omit it, you are intentionally replacing the original behavior, which is rarely what you want in D365FO extension code.
Microsoft’s Chain of Command guidance is explicit that method wrapping is the supported way to extend class behavior in this model. (learn.microsoft.com)
Example: table logic that should stay extensible#
If you are extending a table scenario, prefer the normal table methods instead of the do* variants:
public void insert()
{
// custom logic before insert
next insert();
// custom logic after insert
}That matches Microsoft’s extensibility guidance for tables, because it preserves data events and keeps other extensions able to participate. Using doInsert() would bypass those events. (learn.microsoft.com)
Common mistake: using event handlers when you really need wrapping#
A very common anti-pattern is to reach for an event handler whenever you need to change behavior.
That works fine for “listen and react” scenarios, but it becomes awkward when you actually need to control the flow of an existing method. At that point, an event handler forces you into workaround code, duplicated logic, or fragile assumptions about when the event fires.
The reverse mistake is just as common: people use CoC when the real requirement is only to react to a raised event. That makes the code harder to read than it needs to be.
How I usually decide#
I use a simple rule of thumb:
- Need to wrap behavior? Use CoC.
- Need to respond to a published event? Use an event handler.
- Need new reusable logic? Put it in a small method first, then extend that method if needed.
That last point aligns with Microsoft’s guidance to keep methods short and focused so they remain extensible. (learn.microsoft.com)
Things to watch for#
1. Not every method should be extended#
Microsoft notes that extensibility depends on the method and its intended exposure. In practice, that means you should not assume every method is a safe extension point just because it compiles in an extension class. (learn.microsoft.com)
2. Avoid code that hides the platform events#
If your custom code calls low-level methods that suppress data events, other extensions may stop working as expected. That is one of the reasons Microsoft recommends using the standard table methods in extensible table logic. (learn.microsoft.com)
3. Keep extension methods small#
If your extension needs a lot of logic, split the logic into smaller helper methods. Smaller methods are easier to wrap, easier to test, and less likely to become a maintenance problem later. (learn.microsoft.com)
Recommended approach#
If I were implementing a real D365FO change, I would usually do this:
- Check whether the requirement can be solved by configuration first.
- If code is required, identify the most specific extension point.
- Use CoC when I need to influence the existing method flow.
- Use event handlers when I only need to react to a published event.
- Keep the logic small and avoid bypassing standard table methods unless I have a very specific reason.
That approach gives you code that is easier to upgrade and less likely to interfere with other solutions.
Conclusion#
The practical difference between Chain of Command and event handlers is simple: CoC wraps behavior, while event handlers react to behavior.
If you choose based on the actual requirement instead of habit, your D365FO customizations stay cleaner, easier to troubleshoot, and much more resilient during updates.
Sources#
- Write extensible code — Microsoft Learn
- Class extension - Method wrapping and Chain of Command — Microsoft Learn
- Write extensible methods — Microsoft Learn
- Write extensible tables — Microsoft Learn
- Work with Class extensions in Finance and Operations apps — Microsoft Learn
- Extensibility home page — Microsoft Learn
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