Skip to content

COC (Chain of command) Or Method wrapping in D365 F&O

Microsoft has improved the functionality of class extension for D365FO by adding wrap logic around methods which are defined in the base class that you are augmenting. Apart from this now you can extend the logic of public and protected methods without using event handlers. After wrapping a method, you can access its public and protected methods and variables of that class without making them hookable or using pre or post event handlers.

Note : Features is available in Extension (PU8).

Step 1) I have created a class in model A and add a protected method in it.

Step 2) Create a new class in Model B that will be extension of  DemoParentCOC class and add below code in it.
You will see here that you are now able to call protected method by using next keyword.
The calling stack will execute in such a way that it will first go to Standard method and finds out if there is any extension method. If it will find out any extension then it will call to it. In extension method again it will try to find out  any other extension method. If their is not any  second extension method then it will call first standard method logic.  After executing logic of standard method COCTest of DemoParentCOC class, it will call remaining code of extension method.

If we did not define NEXT word method else it will not execute standard method.

Step 3) After executing the class, it output will be in this way:
– Info : “COC Started” printed
– Info : “Base class” printed
– Info ” “COC ended” printed
Example:

I created  a  COC for Custgroup delete method where I am using context class variable deleteRecord and check if it is set to true. If it is set to True , just giving info as customer group deleted.

[ExtensionOf(tablestr(CustGroup))]
Final class MyCustGroup_extension
{
    public void delete()
    {
        next delete();

        MyCustGroupContext  MyCustGroupContext = MyCustGroupContext::current();
        if(MyCustGroupContext && MyCustGroupContext.deleteRecord)
            info("Customer group  deleted");
    }

}

To test this , created a runnable class where just before calling delete method I am  creating instance of the context class and settingdeleteRecord variable to true.

class TestDisposableContext
{        
    
    public static void main(Args _args)
    {
        CustGroup       custgroup;
        
        CustGroup = CustGroup::find('50' );
        MyCustGroupContext MyCustGroupContext = new MyCustGroupContext();
        MyCustGroupContext.deleteRecord = true;

        if (CustGroup)
        {
            ttsbegin;
            CustGroup.selectForUpdate(true);
            CustGroup.delete();
            ttscommit;
        }
    }

}

When I ran runnable class customer group 50 got deleted and I got the message  which I wrote in COC of method.

Published inTechnical Blog