I resolved it by making use of the OnBeforeAction and OnAfterAction of the standard "Report Statement" Action.
Check the related customer "Custom Report Selection" and see if there is an ID assigned to the Customer Statement.
Then we check the "Report Selections" to see what ID is assigned to Customer Statement.
If the two are not the same then assign the "Custom Report Selection" Report ID to the "Report Statement" Report ID. In my case there are no Email Body changes or Custom Layouts that needs to be assigned.
After the report has been run we reassign the old Report ID back to "Custom Report Selection".
trigger OnBeforeAction()
var
ReportSelections: Record "Report Selections";
CustomReportSelection: Record "Custom Report Selection";
begin
Clear(ReportID);
CustomReportSelection.Reset();
CustomReportSelection.SetRange("Source Type", DATABASE::Customer);
CustomReportSelection.SetRange("Source No.", Rec."No.");
CustomReportSelection.SetRange(Usage, CustomReportSelection.Usage::"C.Statement");
if CustomReportSelection.FindFirst() then begin
ReportSelections.Reset();
ReportSelections.SetRange(Usage, ReportSelections.Usage::"C.Statement");
ReportSelections.SetRange(Sequence, '1');
ReportSelections.SetFilter("Report ID", '<>0');
if ReportSelections.FindFirst() then
if CustomReportSelection."Report ID" <> ReportSelections."Report ID" then begin
ReportID := ReportSelections."Report ID";
ReportSelections.Validate("Report ID", CustomReportSelection."Report ID");
ReportSelections.Modify(true);
end;
end;
end;
trigger OnAfterAction()
var
ReportSelections: Record "Report Selections";
begin
if ReportID <> 0 then begin
ReportSelections.Reset();
ReportSelections.SetRange(Usage, ReportSelections.Usage::"C.Statement");
ReportSelections.SetFilter("Report ID", '<>0');
ReportSelections.SetRange(Sequence, '1');
if ReportSelections.FindFirst() then begin
ReportSelections.Validate("Report ID", ReportID);
ReportSelections.Modify(true);
end;
end;
end;