dynamics-business-centraldynamics-al

AL report Date filtering Business central Development | Date restriction on RequestFilterFields


​ ​ Hello, struggling to learn with AL. I got simple report that is supposed to show data in table.

RequestFilterFields = "Transfer Order Date";

I was hoping to use requestfilterfields to sort out my data. However, there should be minimum value for date value. In this example, 30/06/2024. Report shouldn't show data prior to 01.07.2024. So, i added this trigger:

trigger OnPreDataItem()
            begin
                SetRange("Transfer Order Date", DMY2Date(1, 7, 2024), Today);
            end;

Apparently, this trigger always shows "01.07.2024 ---> Today", despite I enter filter value for the Transfer Order Date. ​What can i do to achieve what i am looking for?


Solution

  • The value entered on the request page is overwritten by the logic in the OnPreDataItem trigger. Take a look at the documentation that illustrates the general overall flow when a report is invoked: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-report-triggers?tabs=current%2Crdlc#overall-report-trigger-and-operations

    In the case you have provided, the following would only apply the SetRange filter if there is not already a filter on the "Transfer Order Date" field.

    trigger OnPreDataItem()
    begin
        if GetFilter("Transfer Order Date") = '' then
            SetRange("Transfer Order Date", DMY2Date(1, 7, 2024), Today());
    end;
    

    EDIT: I have re-read your question and come to the conclusion that my answer above is not accurate.

    Since you wanted to set a minimum filter regardless of whether the user enters a custom filter or not, the following use of the FilterGroup method is what you are looking for:

    trigger OnPreDataItem()
    begin
        FilterGroup(10);
        SetFilter("Transfer Order Date", '>=%1', DMY2Date(1, 7, 2024));
        FilterGroup(0);
    end;
    

    Please note that this type of hidden filter is not visible to the user. Display a note to inform the user that the data has been filtered, otherwise there is a risk that the question will appear later.

    For detailed information about FilterGroup see: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-filtergroup-method