Sometimes you need to make some customizations to default reports. A natural behavior would be saving the report in the 50000 range. For documents like Sales Invoice you can simply change the Report Selections, but what if you need to change for example “Detail Trial Balance” (Report 4) or “Whse. – Posted Receipt” (Report 7308).
The “Detailed Trail Balance” is called from several places like the MenuSuite and the “G/L Account List” page. When it comes to printing the “Whse. – Posted Receipt” you need to make multiple modifications like Codeunit “Warehouse Document-Print” and “Whse.-Post Receipt + Pr. Pos.”.
Modifying all these places is not that interesting to do. You can simply comment the default code and call the new 50000 report instead. Or you can also do it the PRS way by disabling the default code by calling “IF CodeunitxyzHook.OnBeforePrintReport THEN”. And then run the custom report in your hook with an EXIT(FALSE) as last code.
But still you need to make a lot of identical modification.
To avoided all of this I came up with the following idea.
I made my modification direct in the OnInitReport trigger of the default report as you can see.
The trick here is to call for example ReportHook.PrintSalesInvoice and directly close the default report with CurrReport.QUIT. In the ReportHook Codeunit I run my custom report.
A big advantage of this way is that you only modify one object and with one line of code. This is easier and better for future upgrades.
You also don’t need to worry about forgetting a place where the report is called directly.
It’s also faster than searching for all these places.
To make it even more flexible and more reusable I associate the default and the custom report together in a new table. Now my final code will look like this.
Instead of hardcoded print the new custom report I pass the ReportID to my new function “PrintCustomReport”.
In this function I search if there is a custom report for my default and then run it.
Not that spectacular but very handy I think.
Now you can easy copy the following one line of code in every default report that need to be customized and configure the custom report in the settings.