AX7 / D365 Extensions: Add a Field and a Method to a Table
In July I posted my first article regarding extending AX 7, where we talked about how to extend a class. Today I would like to show you how to extend a table.
In this post we will add a new field and also create a extension class to add a new method to the table.
Table Extension – adding a new Field
For this demo I used the InventTable. To add an extension, create a new project in your model, search for a table in the AOT, right click it and select “Create extension”.
Then open the newly created extension and add a new field. The new field is shown in bold, so you can easily distinguish it from the AX7 standard. Basically that is all it takes to extend a table.
Extend the Table with a new Method
To add a new method to the table just create a new class in the project from before.
Like seen in the class extension, we need to define an attribute first. Instead of “classStr”, we use “tableStr”, as we want to extend a table. Don’t forget to make the class final.
[ExtensionOf(tableStr(InventTable))] | |
final class InventTable_Extension | |
{ | |
public static boolean isMyFieldEmpty(InventTable _this) | |
{ | |
return _this.MyExtField == '' ? true : false; | |
} | |
} |
Next we add our new method, a pretty simple one. We just check if the new field is empty or not, if it is empty we return true, else false.
After adding the new method, we want to see the extensions in action. For this purpose I added a new “Runnable Class”, known as “Job” in AX 2012.
In this class I added, besides the main method, two static methods. One method to add a string to our new field, another method to check if the field is filled or not. It is pretty self explaining, but if you have any questions just leave me a comment, I will gladly explain every detail.
class TestMyExtension | |
{ | |
/// <summary> | |
/// Runs the class with the specified arguments. | |
/// </summary> | |
/// <param name = "_args">The specified arguments.</param> | |
public static void main(Args _args) | |
{ | |
InventTable invenTable = InventTable::find("1000" /* ItemId */, true /* Select For Update */); | |
if (invenTable) | |
{ | |
TestMyExtension::InsertMyText(invenTable, "FillMyExtension"); | |
if (TestMyExtension::CheckIfFieldIsEmpty(invenTable)) | |
{ | |
info("Empty!"); | |
} | |
else | |
{ | |
info("Is filled!"); | |
} | |
} | |
} | |
public static void InsertMyText(InventTable _invenTable, str _myExtText) | |
{ | |
try | |
{ | |
ttsbegin; | |
_invenTable.MyExtField = _myExtText; | |
if (_invenTable.validateWrite()) | |
{ | |
_invenTable.update(); | |
} | |
else | |
{ | |
throw Exception::Error; | |
} | |
ttscommit; | |
} | |
catch | |
{ | |
error(":/"); | |
} | |
} | |
public static boolean CheckIfFieldIsEmpty(InventTable _invenTable) | |
{ | |
return InventTable::isMyFieldEmpty(_invenTable); | |
} | |
} |
Run the job, the result should look like this.
Issues
While setup this example I came around a few issues. The first one is obvious, we should sync our database if we add new field. Just select the “Dynamics 365” menu from the top, hit “Synchronize database…” – time for a coffee.
Error: A reference to “Dynamics.AX.Ledger” is required to compile this module.
I also had to reference a few new packages to my model. Todo so, hit “Dynamics 365” from the menu, go to “Model Mangement” and select “Update model parameters”. Select your model and hit next, then add the models that are missing. Take a look to the “Error List” to see which packages are missing, “A reference to “Dynamics.AX.xyz” […]” xyz is usually the package that is missing.