Today I received a call from a partner that was going crazy on a problem during the preview of some statistical reports in Dynamics 365 Business Central.
The report was a custom modification of an existing one (done via a reportextension object). In this reportextension, the partner added some global variables used to perform filters and actions on the report’s dataitem, but the report seems not working correctly on preview (global variables changed in the request page was cleared).
To explain the scenario, let’s consider this simple example:
Here, the reportextension objects adds a boolean parameter (called MyGlobalVar) in the request page initialized to TRUE. Then in the OnAfterAfterGetRecord of the report’s dataitem, we add filters to the dataitem itself accordingly to this global variable value. In this sample, if MyGlobalVar is true, the Customer Top 10 List report shows only customers where Blocked = All. If the value of MyGlobalVar is false, it shows all customers.
What happens if we run this report now?
MyGlobalVar is initialized to true as defined in our AL code:
and if I click on Preview, I see the filtered results:
And if now I change the value of MyGlobalVar to false in the request page and I redo the preview of the report?
I have the wrong result: records are filtered exactly like in the previous scenario:
This problem is not new and it’s related to the report’s multiple preview feature (more info here). The OnOpenPage trigger is executed multiple times.
Unfortunately, this is by design. With the multi-preview mode feature, you should avoid having reports with the following implementations:
- Set global variables in page triggers
- Use the OnQueryClose page trigger to change global variables in the report
- Depend on instance methods to be called to set state variables before report invocation
- The request pages that have
SaveValuesproperty set to
If you have reports that use any of these implementations, refactor them to ensure report content renders as expected.
You should set the filter in the OnAfterPreDataItem trigger. Otherwise the first customer is always printed.
More importantly, how would we make this work in a reportextension? The OnInitReport trigger isn’t available.
Sorry, not understood. The report is right and works. It’s a sample to show a current limitation in preview mode due to multiple preview feature.