Specifically, when the user posts the journal, a message should appear asking if they want to print Report 211. If the user selects "Yes," the Report 211 Request Page should open, pre-filtered for the relevant Customer Ledger Entry.
The issue is that my current code is not filtering the Customer Ledger Entry correctly. Any help would be greatly appreciated.
codeunit 50102 "Print Pmt Rcpt On Post CRJ"
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Line", 'OnAfterPostGenJnlLine', '', false, false)]
local procedure ShowReceiptReportOnAfterPost(var GenJournalLine: Record "Gen. Journal Line")
var
CustLedgerEntry: Record "Cust. Ledger Entry";
ErrorInfo: ErrorInfo;
PrintReceiptQst: Label 'A customer payment was posted. Do you want to show the Customer Payment Receipt report?';
CouldNotFindCLEDsg: Label 'Could not find the corresponding Customer Ledger Entry for Document No. %1.', Comment = '%1 = Document Number';
ReportExecutionErr: Label 'An error occurred while trying to run the report.';
begin
// run this logic for Customer payments from a Cash Receipt Journal.
if GenJournalLine."Journal Template Name" <> 'CASHRCPT' then
exit;
// Ask the user if they want to print the report 211 after posting the journal.
if not Confirm(PrintReceiptQst) then
exit;
// Find the Customer Ledger Entry that was just created.
CustLedgerEntry.SetRange("Document No.", GenJournalLine."Document No.");
if CustLedgerEntry.FindFirst() then begin
// run the report
if not TryRunReceiptReport(CustLedgerEntry) then begin
ErrorInfo.Title := ReportExecutionErr;
ErrorInfo.Message := GetLastErrorText();
Error(ErrorInfo);
end;
end else
// inform the user if the related ledger entry could not be found.
Message(CouldNotFindCLEDsg, GenJournalLine."Document No.");
end;
[TryFunction]
local procedure TryRunReceiptReport(var CustLedgerEntry: Record "Cust. Ledger Entry")
begin
Report.RunModal(Report::"Customer - Payment Receipt", true, false, CustLedgerEntry);
end;
}