How To Write A D365 Display Method - Dynamics 365 Musings (2024)

Share this:


While often the data you want to show on a form is stored in a field on a table, this is not always the case. There are times when the data you need to show must run code to come up with the final output. In this article, I will show you how to write a D365 Display method. Display methods run code based on the data in the current record. And show a value that is not stored directly in a table field.

Explaining Display Methods

On a form, you can show a grid. And on that grid you can show multiple columns such as strings, and numbers, and enum values.

Almost always, the controls in this grid, have the DataSource property set. This means that the grid is showing you records retrieved from the DataSource on the form.

And most of the time, the DataField property is set on the controls in the grid of a form. This means that the control will show you the value stored in the DataSource field specified.

However, there are times when the value that you wish to show is not stored explicitly in a field on the table. In these cases, you can use what is called a Display method. Display methods are very similar to regular x++ methods. However, there are a couple of differences.

  1. They use the keyword ‘display’ in the method definition.
  2. They should return a value that can be displayed in a form control. In almost all cases, this means they should return a basic data type, such as a string, an integer, a real, or an enum. They should not return a class object, as these cannot be display directly on a form. And while it is possible to have display methods return an array, this is not very common.

A Basic Example

To help teach you how to write a D365 display method, I will start with a basic example. Let’s pretend that I have a table and form. On this form, I have a field where I show someone’s First Name. And I show another field where I show someone’s Last Name. What happens if I wish to show a single field that displays the combined First Name and Last Name?

I could create a new field named ‘Full Name’ and override the ‘insert’ method on the table. Then add code to the ‘write’ method, so that every time the record is written to the database, the First Name and the Last Name field values are combined and then stored in the ‘Full Name’ field.

This would work, but I would essentially be stored the same data twice. And while this helpful to do in many cases for the sake of SQL performance, this same thing can be accomplished using a D365 Display method.

Example Display Method Code

The D365 Display method for the above example would look like this.

public display Name FullName(){ Name fullName = this.FirstName + this.LastName; return fullName;}

Notice that the keyword ‘display’ is used in the method definition. Without this keyword, you cannot use this method on a form control or report.

The method returns an EDT of type ‘Name’. As a Best Practice, an ‘EDT’ should always be used as the return type on a display method. You should not use a base data type, such as int, or str, or real.

There are a few benefits of using an EDT.

  1. The EDT has a Label property on it. And so when a display method is used on a form control, that control can inherit the label of the EDT returned on the method. While you can override the label on a form control, having a default label is still very helpful.
  2. Some of the properties on the EDT ultimately affect how the data is shown. For example, if the EDT is of type string, the EDT will have a string length associated with it. The EDT’s string length will help control how many characters of text are shown on the form control.
  3. A display method can actually be added to a table ‘field group’. And that field group can be set on a grid control in a form. The base data type of the EDT on the display method tells the system what type of control to create on the grid. such as String control, or Combo Box, or Checkbox, ect.

A display method must always return a value. This is the value that will be displayed on the form.

You can read Microsoft’s documentation here.

Where To Add A Display Method

D365 Display methods can actually be added to multiple objects. They can be added as Table methods, form methods, form data source methods, report methods, and report design methods.

When a display method is created on a form object, only that form can use that display method. And the same thing when a display method is created on a report.

Therefore, it is recommended that you add the display method to a table whenever possible. Adding the method to a table, allows the display method to be used by any form that has a Data Source that uses that table. This allows for your code to be reused in a lot more places.

Add A Display Method To A Form

The next step to learning how to write a D365 display method is to connect your display method to a form control.

First, create a new control on the grid of your form.

Right click on the control and select ‘Properties‘.

Se the ‘Data Source‘ property to be the name of the Data Source on the form, whose underlying table contains the display method.

Finally, instead of setting the ‘Data Field‘ property, set the ‘Data Method‘ property to be the name of your display method.

How To Write A D365 Display Method - Dynamics 365 Musings (1)

That is it. Now, for each record in the grid, the display method will be run. The display method ‘this’ variable will contain the current record being displayed. It will use values on the record, and the code you have written in the method to produce a result that is shown in the form control.

Product Name Examples

There are many examples where using Display methods is very help.

Imagine you are storing a field which is the primary key on a related record. Typically, in order to show any data on that related record you would need to add the related table as a Data Source. Then add fields from that Data Source to your form. Using a D365 Display Method you can write x++ code to select the related record. Then display a value on the related record.

For example, the ItemId is a field that is stored on many tables. SalesLine is one of those tables. The name of the item is stored on the related table InventTable. Instead of joining to the InventTable, the display method ‘itemName’ on the SalesLine table can be used. This method calls other methods to find the related InventTable record. Then it runs additional logic to determine what text should display as the item name.

 public display SalesLineShowItemName itemName() { return this.inventTable().itemName(this.inventDim()); }

Customer Address example

Let’s look at another example. Most of the customer information is stored on the ‘CustTable’ table. Customers can have many addresses related to them. But they are are only allowed to have one address marked as the ‘primary’ address. In order to retrieve all of the primary address information, several related tables need to be selected.

Instead of doing the work of adding many Data Sources to a form, the display method ‘primaryAddress’ on the CustTable can be used. This method retrieves the related postal address record. Then displays the ‘Address’ field from that table.

 display LogisticsAddressing address() { return this.postalAddress().Address; }

Cautions When Using Display Methods

There are some things that you should be aware of when using a D365 Display method.

Whenever a display method is used on a form, the display method needs to be run once for every record shown on the form. This can have a performance impact. If the display method you have written is time-consuming to run, the form will not be able to show the data quickly. And this will cause the user experience to suffer.

To mitigate this, instead of putting the display method on a control that is on a grid. You can put the display method on another tab, which is not always shown to the user. The display method will only be run for controls that are being shown to the user.

In addition, the filter functionality on a grid control cannot be used on a column that uses a display method. This is an important limitation to pay attention to. Therefore, if it is a requirement that the user be able to filter the control, then you need to not use a Display Method. And instead join in Data Sources, or store the value off into a field that can be used.

The reason for this is because in order for the system to filter the results of a display method, the display method would need to be run against every record in the table. And this would be a huge performance impact. Whereas, one of the things that makes Display Methods practical, is that they are only run on records the users is viewing.

Caching Display Methods

One way that the performance of a display method can be improved is to cache the display method. Microsoft Dynamics 365 for Finance and Operations uses a lot of caching to improve the performance of the forms. However, all display methods are not cached automatically.

Old Way Of Caching A Method

In order to cache the display method, first override the ‘init’ method on the form.

Next, add the a line of code in this format:

<Data Source Name>_ds.cacheAddMethod(tableMethodStr(<TableName>, <Display Method Name>));

For example, there is code on the CustTable form, to cache the edit method named ‘phone’. It is found in the ‘init’ method. It looks like this.

custTable_ds.cacheAddMethod(tableMethodStr(CustTable, phone));

The code uses the method cacheAddMethod on the form Data Source. The name of display method is passed into the method. The code uses the global function ‘tableMethodStr’ to get the name of the method. This global function takes the name of the table, and the name of the method as parameters. This is useful, because if the name of the method ever changes, a compile error will be thrown until this code is changed. Whereas a hard-coded string would only fail at runtime.

Please see additional Microsoft documentation here.

New Way Of Caching A Method.

There is a new way of Caching a method. Instead of overriding the ‘init’ method on a form, and adding a call to ‘cacheAddMethod’, the new way is much simpler. Simply add this attribute just above your method definition in code.

[SysClientCacheDataMethodAttribute(true)]

Summary

In this article I showed you how to write a D365 display method. I explained that a display method is just like a normal method, except that it uses the ‘display’ keyword. Most of the time, Display methods should be added to Table objects in the Application Explorer. And then they are tied to a form control by setting the Data Source and Data Method properties on the control.

Display methods are incredibly useful. They make it much easier to show values on related records. And they allow code to be run and values to be shown that are not stored in any table.

There is a performance impact to using display methods. And you cannot filter on display methods. So developers should keep these limitations in mind before using them.

How To Write A D365 Display Method - Dynamics 365 Musings (2)

Peter Ramer

Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Related


Share this:

How To Write A D365 Display Method - Dynamics 365 Musings (2024)

FAQs

How do I cache a display method? ›

Add a display Method to the Cache

Right-click the data source that the method is associated with, and then select Override Method > init. Call the FormDataSource. cacheAddMethod method after the call to super() in the init method. The first parameter for cacheAddMethod determines the name of the method to be cached.

How do I extend the display method in d365fo? ›

Right click on the control and select 'Properties'. Se the 'Data Source' property to be the name of the Data Source on the form, whose underlying table contains the display method. Finally, instead of setting the 'Data Field' property, set the 'Data Method' property to be the name of your display method. That is it.

What is a display method? ›

Display methods are basically main and important methods that are employed by software inspection process simply to ensure and confirm correctness of code and also to validate formal models.

How do you use the display method? ›

To retrieve data from display methods

Select the node for the dataset. In the Properties window, specify the following values. Dynamics AX - The data source for the dataset must be set to Dynamics AX if you want to set the data source type to an AX Query. Query indicates you will bind the dataset to an AX Query.

What is the difference between edit and display method? ›

Difference Between Edit And Display Method

The display indicates that the method's return value is to be displayed on a form or a report. The value cannot be altered in the form or report. Edit indicates that the method's return type is to be used to provide information for a field that is used in a form.

Which attribute can be used to add caching feature to display method? ›

D365FO allows to use SysClientCacheDataMethodAttribute attribute in the display method declaration. Then forms should cache such methods automatically. And when the display method is used in the form's control, if the cacheDataMethod property set to yes, the display method will be cached.

How do I extract data from cache? ›

To extract a data, we start by looking inside the DataFrame's metadata. If the data is in cache, there is an entrance in the metadata cache with a key or associated path to it. If it's not in the cache, it's extracted and the produced DataFrame is put in cache. Don't forget to add a line in the DataFrame's metadata.

How do I clear my always on display cache? ›

Clear the Always On Display Cache

Tap on Always On Display. Scroll down and select About Always On Display. Next, tap on the i button in the top-right corner of the screen and select the Storage option. Select the Clear cache option at the bottom.

How do I add a lookup method in D365FO? ›

How to create lookup in D365FO using X++
  1. Create a form.
  2. Open the form.
  3. Go to the form string control on which you want to show lookup.
  4. Go to the events of the form string control on forms.
  5. Right click the OnLookup event and click Copy event handler method.

How can I improve my D365FO performance? ›

Tips and tricks for faster F&O performance
  1. Use out-of-the-box functions to prevent system issues, and optimize long-running data queries by adding Microsoft SQL indexes.
  2. Consider performance impact when customizing D365FO. ...
  3. Keep designs minimalist with <75 fields and 5 tabs, and prioritize read-only fields.
Apr 6, 2023

What are the 4 basic types of displays? ›

The four basic types of displays are those that feature one item; similar products; related products; and a cross mix of items. what are the key elements to visual merchandising? why are interior displays important? which type of interior display is most effective for new product introductions?

What are the two main types of display? ›

There are several types of display screens, such as LCD (Liquid Crystal Display), LED (light-emitting diode), OLED (Organic light-emitting diode), AMOLED (Active Matrix Organic light-emitting diode), and E-ink (Electronic Ink) screens.

How do I create a lookup method in D365FO? ›

How to create lookup in D365FO using X++
  1. Create a form.
  2. Open the form.
  3. Go to the form string control on which you want to show lookup.
  4. Go to the events of the form string control on forms.
  5. Right click the OnLookup event and click Copy event handler method.

What is the display method in ax2012? ›

Display Method: Indicates that the methods return value is to be displayed on a forms (or) Reports . The value cannot be altered in the form or report. Take the new method in a table, and then drag that method into the grid and set data source properties.

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 5679

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.