Microsoft Dynamics 365 Business Central allows developers to automate financial processes using AL code. However, while customizing journal posting, I recently faced a tricky issue related to No. Series and General journal line reappearance.
My Scenario
I was developing a custom Commission Posting process to automatically create and post General Journal Lines under:
Template Name: GENERAL
Batch Name: COMMISSION
The No. Series used does not allow gaps, ensuring continuous document numbering.
Here’s the key part of the implementation:
local procedure CreateCommissionGLEntry2()
var
JournalLine: Record "Gen. Journal Line";
GenJnlBatch: Record "Gen. Journal Batch";
NoSeriesMgt: Codeunit "No. Series";
GenJnlPost: Codeunit "Gen. Jnl.-Post";
PeekNo, NextDocumentNo: Code[20];
begin
GenJnlBatch.SetRange("Journal Template Name", 'GENERAL');
GenJnlBatch.SetRange("Name", 'COMMISSION');
GenJnlBatch.FindFirst();
PeekNo := NoSeriesMgt.PeekNextNo(GenJnlBatch."No. Series", WorkDate());
DeleteEarlierUnpostedGenJournalDocs('GENERAL', 'COMMISSION', PeekNo);
NextDocumentNo := NoSeriesMgt.GetNextNo(GenJnlBatch."No. Series", WorkDate(), false);
JournalLine.Init();
JournalLine.Validate("Journal Template Name", 'GENERAL');
JournalLine.Validate("Journal Batch Name", 'COMMISSION');
JournalLine.Validate("Document No.", NextDocumentNo);
JournalLine.Validate("Account Type", JournalLine."Account Type"::Employee);
JournalLine.Validate("Account No.", 'E0001');
JournalLine.Validate("Bal. Account Type", JournalLine."Bal. Account Type"::"G/L Account");
JournalLine.Validate(Amount, -5000);
JournalLine.Insert(true);
GenJnlPost.Run(JournalLine);
end;
To delete earlier unposted entries, I used this helper method:
local procedure DeleteEarlierUnpostedGenJournalDocs(JournalTemplateName: Code[20]; JournalBatchName: Code[20]; PeekNo: Code[20])
var
GenJnlLine: Record "Gen. Journal Line";
begin
GenJnlLine.SetRange("Journal Template Name", JournalTemplateName);
GenJnlLine.SetRange("Journal Batch Name", JournalBatchName);
GenJnlLine.SetFilter("Document No.", '<%1', PeekNo);
if GenJnlLine.FindSet() then
repeat
GenJnlLine.Delete(true);
until GenJnlLine.Next() = 0;
end;
The Problem
Even after deleting all earlier unposted documents, when I run:
GenJnlPost.Run(JournalLine);
Business Central automatically re-populates the deleted lines and then shows the error:
“You have one or more documents that must be posted before you post document no. SC0016 according to your company's No. Series setup.”
It seems BC restores these deleted lines during posting, preventing the new entry from being posted.
What I Tried:
Deleted earlier unposted entries — they reappear during posting.
Allowed gaps in No. Series — works but not acceptable for business rules.
Used PeekNextNo and GetNextNo properly — same issue.
Tried temporary journal lines — posting fails as persistent records are required.
Still, the issue remains: old unposted entries return automatically before posting.