web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Printing a report for the current document no. in general journal lines

(1) ShareShare
ReportReport
Posted on by 2
Hello guys,
 
I have a case in Gen. Journal Line (81), page: Fisc. Payment Journal (61122, Worksheet) when clicking an Post and Print button in the ribbon, it posts the current general journal lines and it also generates an Payment report that prints all the posted general journal lines historically ever posted and not just the prints it for the current document no currently posting. Below you can find the code that does this. NOTE: this code acts like standard code and I do not have access to edit it, so whatever I do I need to work on extensions or new files.
 

action("Post and &Print")

                {

                    ApplicationArea = Basic, Suite;

                    Caption = 'Post and &Print';

                    Image = PostPrint;

                    Promoted = true;

                    PromotedCategory = Category8;

                    PromotedIsBig = true;

                    ShortCutKey = 'Shift+F9';

                    ToolTip = 'Finalize and prepare to print the document or journal. The values and quantities are posted to the related accounts. A report request window where you can specify what to include on the print-out.';

 

                    trigger OnAction()

                    begin

                        CODEUNIT.Run(CODEUNIT::"Gen. Jnl.-Post+Print", Rec);

                        CurrentJnlBatchName := GetRangeMax("Journal Batch Name");

                        SetJobQueueVisibility();

                        CurrPage.Update(false);

                    end;

                } 

 

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Batch Posting Print Mgt.", 'OnBeforeGLRegPostingReportPrint', '', true, true)]

    local procedure "Batch Posting Print Mgt._OnBeforeGLRegPostingReportPrint"(var ReportID: Integer; ReqWindow: Boolean; SystemPrinter: Boolean; var GLRegister: Record "G/L Register"; var Handled: Boolean

    )

    var

        GeneralLedgerEntries: Record "G/L Entry";

        BankAccountLedgerEntriesAmount: Record "Bank Account Ledger Entry";

        BankAccountLedgerEntriesAmountNegative: Record "Bank Account Ledger Entry";

        R1: Report "DTI Payment from Bank";

        R2: Report "DTI Cash Mandate from Bank";

        SelectionMngt: Codeunit SelectionFilterManagement;

        RecRef: RecordRef;

        LastDocumentNo: Code[20];

        TotalDocumentNo: Text;

        TotalPostingDate: Text;

        TotalPostingDateDate: Date;

    begin

        Clear(LastDocumentNo);

        Clear(TotalDocumentNo);

        Clear(TotalPostingDate);

        GeneralLedgerEntries.Reset();

        GeneralLedgerEntries.SetFilter("Entry No.", '%1..%2', GLRegister."From Entry No.", GLRegister."To Entry No.");

        GeneralLedgerEntries.SetCurrentKey("Document No.");

        GeneralLedgerEntries.Ascending();

        if GeneralLedgerEntries.FindSet() then begin

            Clear(RecRef);

            RecRef.GetTable(GeneralLedgerEntries);

 

            Evaluate(TotalPostingDateDate, SelectionMngt.GetSelectionFilter(RecRef, GeneralLedgerEntries.FieldNo("Posting Date")));

            BankAccountLedgerEntriesAmount.Reset();

            BankAccountLedgerEntriesAmount.SetFilter("Document No.", SelectionMngt.GetSelectionFilter(RecRef, GeneralLedgerEntries.FieldNo("Document No.")));

            BankAccountLedgerEntriesAmount.SetFilter("Posting Date", '%1', TotalPostingDateDate);

            BankAccountLedgerEntriesAmountNegative.Copy(BankAccountLedgerEntriesAmount);

            BankAccountLedgerEntriesAmount.SetFilter(Amount, '>%1', 0);

            if BankAccountLedgerEntriesAmount.FindSet() then begin

                Clear(RecRef);

                RecRef.GetTable(BankAccountLedgerEntriesAmount);

                R1.setDocNoFilter(SelectionMngt.GetSelectionFilter(RecRef, BankAccountLedgerEntriesAmount.FieldNo("Entry No.")), '');

                Report.Print(Report::"R1", '');

            end;

 

            BankAccountLedgerEntriesAmountNegative.Copy(BankAccountLedgerEntriesAmount);

            BankAccountLedgerEntriesAmountNegative.SetFilter(Amount, '<%1', 0);

            if BankAccountLedgerEntriesAmountNegative.FindSet() then begin

                Clear(RecRef);

                RecRef.GetTable(BankAccountLedgerEntriesAmountNegative);

                R2.setDocNoFilter(SelectionMngt.GetSelectionFilter(RecRef, R2.FieldNo("Entry No.")), '');

                Report.Print(Report::"R2 '');

            end;

            Handled := true;

        end;

    end;

 
How do I make it that when clicking the button, to make it possible to print the report first for the current document no. posting and the continue to post the lines.
I had a thought to work on but it showed me an error like below:
THANK YOU IN ADVANCE :) 
 
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post+Print", 'OnBeforePostJournalBatch', '', true, true)]
    local procedure "Gen. Jnl.-Post+Print_OnBeforePostJournalBatch"(var GenJournalLine: Record "Gen. Journal Line"; var HideDialog: Boolean)
    var
        R2: Report "Cash Mandate";
        R1: Report "Payment";
        SelectionMngt: Codeunit SelectionFilterManagement;
        RecRef: RecordRef;
        GLRegister: Record "G/L Register";
        BankAccountLedgerEntries: Record "Bank Account Ledger Entry";
        GenJnlPostBatch: Codeunit "Gen. Jnl.-Post Batch";
        ConfirmManagement: Codeunit "Confirm Management";
        JournalTemplate: Code[10];
        JournalBatch: Code[10];
        CurrentDocNo: Code[20];
    begin
        CurrentDocNo := GenJournalLine."Document No.";
        JournalTemplate := GenJournalLine."Journal Template Name";
        JournalBatch := GenJournalLine."Journal Batch Name";
 
        if not ConfirmManagement.GetResponseOrDefault('Do you want to post the journal lines and print the report(s)?', true) then
            Error('');
 
        Codeunit.Run(Codeunit::"Gen. Jnl.-Post Batch", GenJournalLine);
 
        // Clear(GenJournalLine);
        // GenJournalLine.SetRange("Journal Template Name", JournalTemplate);
        // GenJournalLine.SetRange("Journal Batch Name", JournalBatch);
        // GenJournalLine.SetRange("Journal Template Name", JournalTemplate);
        // GenJournalLine.SetFilter(Amount, '<%1', 0);
        // if GenJournalLine.FindSet() then begin
        //     Clear(RecRef);
        //     RecRef.GetTable(GenJournalLine);
        //     R1.setDocNoFilter(SelectionMngt.GetSelectionFilter(RecRef, GenJournalLine.FieldNo("Document No.")), GenJournalLine."Journal Template Name", GenJournalLine."Journal Batch Name");
        //     R1.Run();
        // end;
        BankAccountLedgerEntries.Reset();
        BankAccountLedgerEntries.SetRange("Document No.", CurrentDocNo);
        BankAccountLedgerEntries.SetRange("Journal Batch Name", JournalBatch);
        BankAccountLedgerEntries.SetFilter(Amount, '<>%1', 0);
        if BankAccountLedgerEntries.FindSet() then
            repeat
                Clear(RecRef);
                RecRef.GetTable(BankAccountLedgerEntries);
                R1.SetDocNoFilter(SelectionMngt.GetSelectionFilter(RecRef, BankAccountLedgerEntries.FieldNo("Document No.")), GenJournalLine."Journal Template Name", BankAccountLedgerEntries."Journal Batch Name");
                R1.Run();
                if BankAccountLedgerEntries.Amount < 0 then begin
                    Clear(R2);
                    R2.SetDocNoFilter(SelectionMngt.GetSelectionFilter(RecRef, BankAccountLedgerEntries.FieldNo("Document No.")), GenJournalLine."Journal Template Name", BankAccountLedgerEntries."Journal Batch Name");
                    R2.Run();
                end;
            until BankAccountLedgerEntries.Next() = 0;
        HideDialog := true;
        Message('The journal lines were successfully posted.');
    end;
 
 
 
 
 
Screenshot 2026-05-29 134557.png
I have the same question (0)
  • Suggested answer
    Nour_K Profile Picture
    71 on at

    Hello,

    The issue you are facing in your first attempt happens because using SelectionFilterManagement on active ledger entries right after a commit can sometimes pull a wider filter range than intended, accidentally sweeping in historical data.

    In your second attempt (OnBeforePostJournalBatch), your code throws an error because you are manually calling Codeunit.Run(Codeunit::"Gen. Jnl.-Post Batch", GenJournalLine);. Since the standard "Post and Print" action already invokes the posting batch, executing it again inside the subscriber creates a nested posting loop conflict, which Business Central blocks to protect data integrity.

    The cleanest, most stable approach is to stick to your first event subscriber (OnBeforeGLRegPostingReportPrint), but instead of using selection filters, look directly at the specific GLRegister bounds created for this active transaction. The register explicitly knows the exact entry numbers created during the posting sequence.

    Here is whatyou can do to replace your subscriber:

     

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Batch Posting Print Mgt.", 'OnBeforeGLRegPostingReportPrint', '', true, true)]
    local procedure "Batch Posting Print Mgt._OnBeforeGLRegPostingReportPrint"(var ReportID: Integer; ReqWindow: Boolean; SystemPrinter: Boolean; var GLRegister: Record "G/L Register"; var Handled: Boolean)
    var
        GLEntry: Record "G/L Entry";
        BankAccountLedgerEntry: Record "Bank Account Ledger Entry";
        R1: Report "DTI Payment from Bank";
        R2: Report "DTI Cash Mandate from Bank";
        DocNoFilter: Code[20];
    begin
        GLEntry.Reset();
        GLEntry.SetRange("Entry No.", GLRegister."From Entry No.", GLRegister."To Entry No.");
        if not GLEntry.FindFirst() then
            exit;
        DocNoFilter := GLEntry."Document No.";
        if DocNoFilter = '' then
            exit;
     
        Handled := true;//this part is important
     
        //  Filter and print positive bank ledger entries via R1
        BankAccountLedgerEntry.Reset();
        BankAccountLedgerEntry.SetRange("Document No.", DocNoFilter);
        BankAccountLedgerEntry.SetRange("Posting Date", GLEntry."Posting Date");
        BankAccountLedgerEntry.SetFilter(Amount, '>%1', 0);
        if not BankAccountLedgerEntry.IsEmpty then begin
            Clear(R1);
            // Pass the explicit document string directly
            R1.setDocNoFilter(DocNoFilter, ''); 
            R1.Run();
        end;
        // Filter and print negative bank ledger entries via R2
        BankAccountLedgerEntry.Reset();
        BankAccountLedgerEntry.SetRange("Document No.", DocNoFilter);
        BankAccountLedgerEntry.SetRange("Posting Date", GLEntry."Posting Date");
        BankAccountLedgerEntry.SetFilter(Amount, '<%1', 0);
        if not BankAccountLedgerEntry.IsEmpty then begin
            Clear(R2);
            R2.setDocNoFilter(DocNoFilter, '');
            R2.Run();
        end;
    end;
     
    Hope this helps solve your case!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,216 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,471 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,369

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans