Chain Of Command In D365 Fundamentals

Share this:

What is Chain of Command in D365? Chain of Command is the term that describes how we customize, or extend, base Microsoft code in Microsoft Dynamics 365. Microsoft’s base objects and code cannot be changed directly in D365. However, we are able to make changes to separate objects and classes that are then combined with the existing base object to form the final version.

Why Can’t We Modify Base Microsoft Objects?

In Microsoft Dynamics 365, Microsoft re-architected the code and made it so base objects cannot be changed. In versions 2012 and before, users could modify the base Microsoft source code directly. The problem is that anytime Microsoft released a new hotfix or new features, the new code would need to be merged in with the customized code. This was a very time consuming process.

Now in D365, since the base code cannot be modified, Microsoft can safely deploy updated versions of the base code, that contain hotfixes and new features. Unlike before, custom code will not be overridden, because all customizations must be done in separate, but related objects.

This is a huge benefit as it allows users of Microsoft Dynamics 365 to take hotfixes and new features much faster, easier and safer, than they ever could before. There is still some risk that the new version of the base code once combined with any custom code will work differently than before. Therefore testing is still needed. But this new extension model essentially makes it so no code merge is needed.

When To Use Chain of Command

In order to modify a base Microsoft object in Dynamics 365, we need to use the tools that Microsoft provides to ‘extend’ the base objects. Table and form AOT nodes can be extended using additional Extension nodes in the AOT. However, if you want to extend x++ code, you need to use either Event Handlers or Chain of Command.

While Event Handlers still have their place and are supported. As a developer, I personally find that using Chain of Command is easier to write and read.

Chain Of Command Basics

Microsoft has written some really good documentation on Chain Of Command. I will reference and expand of some of their examples.

Class extension – Method wrapping and Chain of Command – Finance & Operations | Dynamics 365 | Microsoft Docs

Let’s look at a very basic example. Consider you have a base Microsoft class that looks like this:

class BusinessLogic1
{
    str doSomething(int arg) 
    {
        // ...
    }
}

As a developer, you are not allowed to change any code in the method ‘doSomething’ in D365. However, using Chain of Command you can create a new class and write code that will run before or after the base method.

[ExtensionOf(classStr(BusinessLogic1))]
final class BusinessLogic1_Extension
{
    str doSomething(int arg) 
    {
        // Part 1
        var s = next doSomething(arg + 4);
        // Part 2
        return s;
    }
}

Now, anytime the system calls the method ‘doSomething’, it will actually call any Chain of Command class containing ‘doSomething’. This will call your custom code, while also calling the base Microsoft version of the method.

This is extremely powerful as your custom code can do the following:

  • Take any values passed in as parameters, and change them before they call the base Microsoft code.
  • Add code that runs in addition to the base Microsoft code.
  • Retrieve any return value from the base Microsoft code, and potentially change the value before returning it to the code that called this method.

Chain of Command Rules

There are a few required pieces that need to be done in order for the compiler to recognize our new class as a Chain of Command class.

  • The name of the new class you create must end with the text ‘_Extension’. In the example above, the base Microsoft class was named ‘BusinessLogic1’. The name of the class we created was ‘BusinessLogic1_Extension’.
    Pro Tip: Because classes and forms and tables can be named the same thing, and each can be modified using Chain of Command, I have found it helpful to put the object type in the name of the Chain of Command class. This is not required, however, it can make your code more readable. I recommend using a format like this: <BaseObjectName>_<ObjectType>_Extension
  • The keyword ‘final’ needs to be used in the class definition line.
  • The class needs to have the attribute
    [ExtensionOf(classStr(<NameOfBaseClass>))]
    The “classStr” text above will change depending on the type of base objecting you are extending. I will explain more later.
  • Add the method definition the exact same way it appears in the base class.
  • Your code must call the base class’s method inside your extension method using the ‘next’ keyword. Using the above example you need to have this line.
    var s = next doSomething(arg);

Objects That Can Use Chain Of Command

There are several different objects in Microsoft Dynamics 365 that can use Chain Of Command. I will list the most common here.

  • Classes
  • Tables
  • Forms
  • Datasource on a form.
  • Data field on a form.
  • Control of a form.
  • Data entities

Conclusion

You have learned what Chain Of Command (CoC) is and how it allows a developer to add custom code without changing the base Microsoft code. You have learned how to write a basic class. In the next article, I will show you more detailed examples of how to create Chain Of Command classes for each base object type.

Peter Ramer
Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Share this:

13 thoughts on “Chain Of Command In D365 Fundamentals

Add yours

  1. Hello There. I found your bloig using google.
    This is an extremely well writtwn article. I’ll make sure to bookmark it and return to read more of your useful information. Thanks ffor the
    post. I’ll certainly return.

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑