Dynamics 365 Finance & Operations - Chain of command new featuresBy Gustavo Camargo, Senior Technical Consultant, Arbela Technologies
Microsoft had introduced the Chain of Command with Platform Update 9. 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.
|Explore related content: How to Sort a View by Hidden Column|
As of PU15, Microsoft has implemented Chain on command across Classes, Tables, and Forms. However, there have been limitations around form data source\data field methods. At recent Microsoft Business Application Summit, we were introduced to the chain of commands new ability to extend data sources and data field methods. This would be released as part of October 2018 release.
Typical Code structure on a form:
The code in a form of a bunch of nested classes that represents form controls\data sources\data fields. These methods have been not extendable and there are very limited event handlers for the controls and data sources compared to AX 2012. The code structure is certainly represented in form of classes but was not extendable. Developers tend to do over layering or writing humongous code for mini modifications. With the October Release, D365FO has a solution to this over-layering issue. Chain of command on form data sources, data fields methods.
Chain of command syntax for data source fields and forms are bit different compared to classes or tables. We would need to create a separate class for every single object you want to extend. This can be a little cumbersome at the beginning, but once you get the hang of it, the process will become easier.
With CoC, this is how the sample form behaves:
- Create a new record invokes initValue method at data source CustGroup, setting the Terms of Payment to Net10
- Entering the Customer group will trigger the modified method, which will set the Description to TEST
- When the Execute button is clicked, no action is performed as the clicked method is just calling super.
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.
Let’s take a look at the following three classes that will demonstrate how Chain of Command works on data sources, data sources fields, and controls:
- DataSource extension class :
- Data source field extension class:
- Button control extension class:
This is how the form works with the chain of command:
- Create a new record for customer group record should have defaulted to Terms of Payment to Net10. But with Coc, we were able to override the logic to use Net30 as the new Term of payments.
- Similar to init value we have extended the modified method where the description was supposed to be set to TEST and now with the it is modified to a string ‘TEST with added description after coc’
When the Execute button is clicked a Post event is triggered, if the customer group is equal to 10 or 20, then the logic will execute. In the case of this button, it will invoke a method called execute. This method will print a message saying “After CoC: Operation executed through CoC”.
Now, some of you may be wondering, why did he use the clicked method instead of using CoC directly over execute method? Unfortunately, it is NOT possible to add CoC to wrap methods that are not defined in the original base behavior of the nested control type. But as you see, you can invoke that method within an original base behavior method like clicked.
With that said, here is the result:
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.
- Currently, there is no support in the Visual Studio X++ Editor to discover the methods candidates that can be wrapped, so you need to refer to the system documentation for each nested concept to identify the proper method to wrap and its exact signature.
- 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.