Friday, November 17, 2017

Dynamics 365 - Extensibility on methods in a subclass that are declared in the base class


We stumbled upon this while reviewing the extensibiliy requests that we have to submit to Microsoft. As you probably all know, we can no longer overlayer in D365, so we have to adopt the new, extensibility-based approach.

In the class 'AssetProposalDepreciation' we have a customization in the 'run' method. While reviewing this customization, we reasoned that the customization can be moved to a post event on the method 'initFirstDate', which is called just before our customization. However, the class 'AssetProposalDepreciation' is a subclass of the class 'AssetProposal', and the method 'initFirstDate' is declared in the base class (and not overridden in the subclass).

Before I continue, I should mention that the approach below has been tested using the community tech preview (CTP) of D365 fall release 2017.

Attempt 1


The first thing we tried was creating an event handler class with a post event on the method 'initFirstDate' for the class 'AssetProposalDepreciation':

class MyEventAssetProposalDepreciation
{
[PostHandlerFor(classStr(AssetProposalDepreciation), methodStr(AssetProposalDepreciation, initFirstDate))]
public static void AssetProposalDepreciation_Post_initFirstDate(XppPrePostArgs args)
{
//
info("Test"); // Let's assume that this is our customization.
//
}
}

The code above resulted in a compile error; it is not possible to create a post event on the method 'initFirstDate', as it is not declared in the class 'AssetProposalDepreciation' (it is declared in the base class 'AssetProposal').

Attempt 2


The next thing we tried was using Microsoft's new Chain of Command feature. For those of you who are unfamiliar with this new feature, this short video explains it fairly well: https://blogs.msdn.microsoft.com/mfp/2017/07/04/extensible-x-chain-of-command/. So, we created the following extension class:

[ExtensionOf(classStr(AssetProposalDepreciation))]
class MyAssetProposalDepreciationClass_Extension
{
    // The method signature was copied from the class 'AssetProposal'.
    public AssetTransDate initFirstDate(AssetTransDate _assetTransDate, AssetId _assetId) 
    {
        AssetTransDate ret = next initFirstDate(_assetTransDate, _assetId);

        //
        info("Test"); // Again, this is our customization.
        //

        return ret;
   }
}

The code above works :-). 


Whenever the method 'initTransDate' is called in the class 'AssetProposalDepreciation' (e.g., this.initTransDate(...) ), the line 'AssetTransDate ret = next initFirstDate(_assetTransDate, _assetId);' in the extension class is executed first, then the original code in the method 'initFirstDate' is executed (because of the 'next' statement; the return type is assigned to the variable 'ret'), and finally our customization code is executed. 

So, using the Chain of Command feature, we have basically 'mimmicked' a post event. 

No comments:

Post a Comment