AX7 / D365 Extensions: Add a Method to a Class

Today I want to start a small series of blog posts. In this series I would like to show you how to extend AX 7 / D365 objects, without overlayering them, starting with an easy class extension.

Class extension 1.0

For this demo I created an extreme simple class, it only contains one method, which just puts out an infolog.

class BaseClass
public void BaseClassMethod()
info("Hello from the origin!");
view raw BaseClassDemo1 hosted with ❤ by GitHub

To extend this class we have to add a new class to our project. I named it like the class to extend and added an “_Extension” to it. There are two important things to do. First we have to add the [ExtensionOf] attribute, second we have to make the extension class final. I also put in a new method, which also will info a slightly different message than the base class.

final class BaseClass_Extension
public void ExtensionMethod()
info("Hello from the extension!");
view raw BaseClassDemo2 hosted with ❤ by GitHub

Next I added a runnable class (in AX2012 a “Job”) to my project.

class RunMyClasses
static void main (Args _args)
BaseClass baseClass = new BaseClass();
view raw BaseClassDemo3 hosted with ❤ by GitHub

In the screenshot below you can see how the IntelliSense in Visual Studio shows the “ExtensionMethod”, a very neat feature.

D365 Class Extension IntelliSense

If you run the job, you should see the following result.

D365 Class Extension Infolog

As you can see, it is very easy to add a new method to an existing class, without overlayering the original class. I admit this example is a very easy one, but I think you can build upon it. Let me know if you have any question.

In the next post we will take a look on tables.

You may also like...