
In Dynamics 365 Finance and Operations (D365 FO), it’s common to need views that dynamically filter records based on the current date. For example, a view might need to retrieve only those records where:
- The
ValidFromfield is less than or equal to today’s date. - The
ValidTofield is greater than or equal to today’s date.
However, setting up such filters directly in an AOT view can be tricky because:
- AOT filters like
today()are evaluated statically at design time, not dynamically at runtime. - Directly filtering on computed columns in the same view is not always straightforward.
This article demonstrates a modular and scalable solution using two AOT views to implement dynamic date filters.
.
The Two-View Solution
To implement dynamic filtering based on today’s date, you can structure your solution as follows:
- Base View: This view calculates a field (
IsValidRange) to determine whether a record falls within the date range. - Filtered View: This view consumes the base view and applies a filter on the
IsValidRangefield to retrieve only the valid records.
This separation ensures that the logic is modular and reusable.
Step 1: Creating the Base View
The base view is responsible for computing the IsValidRange field, which indicates whether a record’s date range is valid.
Steps to Create the Base View
- Create the View:
- Go to Application Explorer > Data Model > Views.
- Create a new view (e.g.,
BaseView).
- Add Data Sources:
- Add the table containing
ValidFromandValidTofields (e.g.,MyTable) as the primary data source.
- Add the table containing
- Define a Computed Column:
- Add a method in the Methods section of the view to compute the
IsValidRangefield dynamically:
- Add a method in the Methods section of the view to compute the
public static str isValidRangeComputed()
{
return 'CASE
WHEN ValidFrom <= GETDATE()
AND ValidTo >= GETDATE()
THEN 1
ELSE 0
END';
}
- Add the Computed Column:
- Go to the Fields section of the view.
- Add a new field named
IsValidRangeand link it to the methodisValidRangeComputed.
Result of the Base View
The BaseView will now compute the IsValidRange field for all records, which evaluates to:
1if the record falls within the valid date range.0otherwise.
The SQL generated for the BaseView will look like this:
SELECT *,
CASE
WHEN ValidFrom <= GETDATE()
AND ValidTo >= GETDATE()
THEN 1
ELSE 0
END AS IsValidRange
FROM MyTable;
Step 2: Creating the Filtered View
The filtered view uses the base view as its data source and filters records based on the IsValidRange field.
Steps to Create the Filtered View
- Create the View:
- Go to Application Explorer > Data Model > Views.
- Create a new view (e.g.,
FilteredView).
- Add the Base View as a Data Source:
- Add
BaseViewas the primary data source.
- Add
- Add a Range on the
IsValidRangeField:- Expand the
BaseViewdata source in the filtered view. - Add a range to filter records where
IsValidRange = 1.
- Expand the
Result of the Filtered View
The FilteredView will filter out records that are not valid based on today’s date. The SQL generated for the FilteredView will look like this:
SELECT *
FROM (
SELECT *,
CASE
WHEN ValidFrom <= GETDATE()
AND ValidTo >= GETDATE()
THEN 1
ELSE 0
END AS IsValidRange
FROM MyTable
) AS BaseView
WHERE IsValidRange = 1;
Why Use Two Views?
- Modularity: The computation logic for
IsValidRangeis centralized in the base view, making it reusable for other scenarios. - Scalability: You can create multiple filtered views based on the base view for different filtering requirements.
- Maintainability: Any changes to the date range logic only need to be made in the base view.
Testing the Solution
- Compile Both Views:
- Ensure that both
BaseViewandFilteredViewcompile without errors.
- Ensure that both
- Preview Data:
- Use the Preview Data option in AOT to verify that the
FilteredViewretrieves only records withIsValidRange = 1.
- Use the Preview Data option in AOT to verify that the
- Integrate with Other Modules:
- Use the
FilteredViewin forms, reports, or data entities to dynamically retrieve valid records based on today’s date.
- Use the
Conclusion
By using two AOT views—one for computation and one for filtering—you can effectively implement dynamic date filtering in Dynamics 365 FO. This approach not only addresses the limitations of AOT filters but also ensures a clean, maintainable, and scalable solution for handling dynamic conditions.
Leave a comment