Pages

Sunday, February 19, 2017

Class extensions in X++

Suggested way to develop Dynamics 365 is not touching base code. Avoid overlayering is good practice especially in Azure environment. If you would like to add code for example to CustTable you can use extension methods. Below code is showing how to do it.

Note to ExtensionOf attribute, final keyword and _Extension suffix to the name of class.

[ExtensionOf(tableStr(CustTable))]
final class CustTable_Extension
{
    public str formatedName()
    {
        return strFmt('[%1] %2', this.AccountNum, this.name());
    }
}

And here example how to call extension class.

public static void main(Args _args)
{   
    CustTable       custTable;
    select firstonly custTable;
    info(custTable.formatedName());
}

And here example how to call extension class. The same we can do to class. Just change ExtensionOf attribute. Here we are extending MainClass.

[ExtensionOf(classStr(MainClass))]
final class MainClass_Extension
{
    public void sayHello()
    {
        info("Say hello from extension class");
    }
}

Here you can read more details.