
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:
- First Form: The data source is
DirPartyPostalAddressView. - 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 typeDirPartyPostalAddressViewand retrieve thelocationIdfield from it. - Applying the filter: In the second form’s
init()method, we get theQueryBuildDataSourcefor theOTS_CustWeekPickCapacitydata source and add a range (filter) based on thelocationIdfield. The filter value is set to thelocationIdpassed 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