Dynamics 365 Finance & Operations – How to use chain of command in X++ (CoC)
Chain of Command (CoC) enables strongly typed extension capabilities of public and protected methods. It is an amazing piece of development capability that allows technical consultants to extend the application avoiding over-layering.
Microsoft has implemented Chain on command across Classes, Tables, Forms, form data source and data field methods.
Before we dive into the Chain of Command methods, remember that in order to use CoC you must declare your class as final, and your methods should always contain the next keyword.
The next keyword behaves like a super, and it will define when your extended logic executes. The next call after your code behaves like a Pre-event handler, your logic executes first, and later on, the logic residing in the original method gets executed. The next call before your code behaves like a Post event handler, your logic executes after the code residing in the original method gets executed.
How to use chain of command to extend the class method
In below screenshot i am using method wrapping or class extension of standard class:
1.We need to se if the method that we want to extend is public or protected, in our exemple modifyVoucherDate is public.
2. Create a final class that extends PurchFormLetter_ApproveJournal class.
3. If we want to costumize the process before the modifyVoucherDate method is executed, write our code before next keyword otherwise after next keyword.
We can use chain of command in tables, forms, form datasource etc in the same way as we use it for class method, the only thing that change is keyword inside ExtensionOf.
Benefits Of Using Change Of Commands instead of Old Overlayering approach in Microsoft Dynamics 365 finance and operations
- Easy Future upgrades and version enhancements as you are not doing overlayering in standard classes or methods or objects & keeping you separate extension classes.
- Neat & Clean development as no need to compile whole code librarary.
I hope this article provided some light about the upcoming changes on Chain of Command for D365 for Finance and Operations Fall Release.
To wrap up, let’s review some of the requirements and considerations while writing CoC methods on form nested concepts extensions:
- As any other CoC method, these ones need to call always next to invoke the next in the chain, so it can go all the way to the kernel / native implementation in the runtime behavior. This is equivalent as calling super( ) from the form itself to guarantee the base behavior in the runtime is always executed as expected.
- It is NOT possible to add CoC to wrap methods that are not defined in the original base behavior of the nested control type. However, it is possible from the control extension to make a call into this method if it has been defined as public or protected.
- It is NOT needed to recompile the module where the original form is defined to have support for CoC methods on nested concepts on that form from an extension.