How to Filter a Form’s Data Source Using X++ in D365 F&O

Example:
"In D365 F&O, filtering a form’s data source programmatically allows for dynamic and context-sensitive displays. This guide will show you how to filter a form's data source using X++."


Step 1: Overview of the Code

  •  

Step 2: Example Code to Filter Data Source

YourMainTable _yourMainTable;
YourRelatedTable _yourRelatedTable;
str recordId; // Access the data source for filtering
QueryBuildDataSource qbds = YourTargetTable_ds.query().dataSourceTable(tableNum(YourTargetTable)); // Check the record passed in args and apply the filter
if (element.args().record().TableId == tableNum(YourMainTable))
{    
   _yourMainTable = element.args().record();    
       if (_yourMainTable)    
    {          // Clear existing ranges and apply the new filter        
               qbds.clearRanges();        
               qbds.addRange(fieldNum(YourTargetTable, ReferenceField)).value(queryValue(_yourMainTable.ReferenceField));    
    }
}

Step 3: Explanation of the Code

  1. Accessing the Data Source
    • The YourTargetTable_ds.query().dataSourceTable() method retrieves the QueryBuildDataSource object for the specified table.
  2. Clearing Existing Ranges
    • Use qbds.clearRanges() to remove previously applied filters.
  3. Adding a New Filter
    • The addRange() method applies a new filter.
    • queryValue() ensures proper handling of the field's value.
  4. Using Arguments to Determine Context
    • The element.args().record() method checks if a record was passed to the form.

Comments

Popular posts from this blog

How to Override the Lookup Method in D365 X++ Form String Control

How to Open a Form and Send Records as Arguments in D365 F&O