While planning to use display or edit methods in Dynamics is something that should generally make you consider if you perhaps could design your solution a different way, sometimes they are the best way to go.
In previous versions of Dynamics and Axapta, it was very easy to create display or edit methods on tables and forms, but when I recently happened to have to make my first edit method in Dynamics 365, I discovered that the procedure for doing so is somewhat different.
There are evidently several valid approaches, but the one I find best (both in terms of intuitivity and code prettiness) is to use a class extension. Yes, you can use class extensions to add methods to other element types than classes – in this case a table, but it works for forms as well 🙂
First, create a new class. You can name it anything you want, but for some reason it must be suffixed “_Extension”. Let’s say you need to add a display method to CustTable, you could for example name it MyCustTable_Extension.
The class must be decorated with ExtensionOf to let the system know what you’re extending, like so:
[ExtensionOf(tableStr(CustTable))] public final class MyCustTable_Extension { }
Now you can just implement your display method in this class, like you would have done directly on the table in earlier versions of Dynamics – “this” even references the table, so you can access fields and other methods.
For example, the class with a simple (and completely useless) display method that just returns the account number of the customer could look like this:
[ExtensionOf(tableStr(CustTable))] public final class MyCustTable_Extension { public display CustAccount displayAccountNum() { ; return this.AccountNum; } }
Now, to add the display method to a form (or form extension if you can’t edit the form directly), you need to add a field to the form manually and make sure to use the correct type (string in this example).
Then, on the control you would set DataSource to CustTable (or whatever the name of your CustTable data source is) and DataMethod to MyCustTable_Extension.displayAccountNum (make sure to include the class name, otherwise the compiler can’t find the method).
And you’re done 🙂