AX / D365FO – How to Open a Filtered Form Using a Menu Item in D365FO

In Dynamics 365 Finance and Operations (D365FO), it’s common to have a scenario where you want to pass specific values (like a filter) from one form to another when navigating between them. This article will guide you through the process of opening a second form, filtered by a specific field (e.g., locationId), from the first form using a Menu Item of type “Display.”

Scenario Overview

We have two forms in our scenario:

  1. First Form: The data source is DirPartyPostalAddressView.
  2. Second Form: The data source is OTS_CustWeekPickCapacity.

Both data sources contain the locationId field, and we want to pass the locationId from the first form’s selected record to the second form as a filter.

Step 1: Setting Up the Menu Item in the First Form

The first step is to add a Menu Item Button that opens the second form when clicked. This Menu Item should be of type Display.

  • In the first form, add a Menu Item Button.
  • Set the Menu Item Type to Display.
  • Set the Menu Item Name to the name of the menu item that opens the second form.
  • Make sure the DataSource of the button is set to the appropriate data source (in this case, DirPartyPostalAddressView).

By doing this, when the user selects a record and clicks the button, the form linked to the menu item will be opened, and the selected record will be passed to the second form.

Step 2: Passing the Filter to the Second Form

In the second form, we need to capture the locationId from the passed record and apply it as a filter on the second form’s data source (OTS_CustWeekPickCapacity).

Here’s the X++ code you will need in the init() method of the second form to capture the filter and apply it:

public void init()
{
    DirPartyPostalAddressView selectedRecord;
    QueryBuildDataSource qbds;
    QueryBuildRange qbr;

    super();

    // Check if arguments are passed and if it's the correct type
    if (this.args() && this.args().record() && this.args().record() is DirPartyPostalAddressView)
    {
        // Get the selected record
        selectedRecord = this.args().record() as DirPartyPostalAddressView;

        // Get the QueryBuildDataSource for the second form's data source
        qbds = OTS_CustWeekPickCapacity_ds.query().dataSourceTable(tableNum(OTS_CustWeekPickCapacity));

        // Add a range based on the locationId field
        qbr = qbds.addRange(fieldNum(OTS_CustWeekPickCapacity, locationId));
        qbr.value(queryValue(selectedRecord.locationId));

        // Execute the query to apply the filter
        OTS_CustWeekPickCapacity_ds.executeQuery();
    }
}

Explanation of the Code

  • Capture the passed record: The args() method is used to retrieve the arguments passed when opening the second form. We check if the passed record is of type DirPartyPostalAddressView and retrieve the locationId field from it.
  • Applying the filter: In the second form’s init() method, we get the QueryBuildDataSource for the OTS_CustWeekPickCapacity data source and add a range (filter) based on the locationId field. The filter value is set to the locationId passed from the first form.
  • Execute the query: Finally, we call executeQuery() to apply the filter and load the data for the second form with the appropriate filter applied.

Conclusion

This solution allows you to seamlessly pass a filter (in this case, locationId) from one form to another using a Menu Item of type “Display” in Dynamics 365 Finance and Operations. By doing this, you can ensure that the second form is filtered based on the user’s selection in the first form, providing a more efficient and streamlined user experience.

By following these steps, you can easily pass specific values between forms and filter data dynamically in D365FO.

Leave a comment