Miklix

Add Display or Edit Method via Extension in Dynamics 365

Published: July 1, 2022 at 5:51:01 PM UTC

In this article, I explain how to use a class extension to add a display method to a table and a form in Dynamics 365 for Operations, X++ code examples included.


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, occasionally 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, a 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 :-)

Update: It is no longer necessary to include the extension class name when adding the display method to a form, but at the original time of publishing, it was. I'm leaving the information here in case some readers are still using older versions.

Share on BlueskyShare on FacebookShare on LinkedInShare on TumblrShare on XShare on LinkedInPin on Pinterest

Mikkel Bang Christensen

About the Author

Mikkel Bang Christensen
Mikkel is the creator and owner of miklix.com. He has over 20 years experience as a professional computer programmer/software developer and is currently employed full-time for a large European IT corporation. When not blogging, he spends his spare time on a vast array of interests, hobbies, and activities, which may to some extent be reflected in the variety of topics covered on this website.