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...
Answered

Extend Purchase Invoice posting to add custom Gen. Journal Line with Preview Post, rollback support

(6) ShareShare
ReportReport
Posted on by 3,210 Super User 2026 Season 1

Hi Community,

I have a requirement to extend the Purchase Invoice posting process.

Whenever I post a Purchase Invoice, I need to:

  1. Create a negative amount line in the Gen. Journal for each posted Purchase Line (to offset the standard G/L entry).
  2. Split the positive amount across multiple dimensions and IC Partners (similar to allocation accounts), and post those entries as positive Gen. Journal Lines.
  3. Ensure these custom journal entries are posted together with the Purchase Invoice — meaning they should rollback if the invoice posting fails.
  4. Show these journal entries in the Preview Posting as well, without actually posting them.

Currently, I’m currently subscribing to these events:

  • OnAfterPostPurchLine (to create and insert negative Gen. Journal Lines)

  • OnAfterPostPurchaseDoc (to post the journals after the invoice is posted)

This works for normal posting, and the G/L Entries appear correctly when I use Find Entries.

However, this approach does not work in Preview Posting, because my custom Gen. Journal Lines are only created/posted after the invoice posting is complete.

I want to change my approach so that:

  • The custom Gen. Journal Lines (negative + split positive entries) are created and posted along with the Purchase Invoice in the same posting transaction.

  • They are included in the Preview Posting entries.

  • They rollback automatically if invoice posting fails.

Is there a better event or pattern (e.g., using the posting buffer or Preview Posting handler) that I can subscribe to, so my custom journal lines behave as if they were part of the standard Purchase Invoice posting?

Any guidance or best practice on how to achieve this would be greatly appreciated!

I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    99,857 Super User 2026 Season 1 on at
    This customization is very risky. It at least leads to inconsistencies between the amounts in the Vendor Ledger Entries and the GL Entry.
    If you need to add a negative line, it is safer to do it on the Purchase Invoice before posting.
     
    Hope this helps.
    Thanks.
    ZHU
  • Suggested answer
    Valentin Castravet Profile Picture
    32,431 Super User 2026 Season 1 on at
    Agree with Yun Zhu, this is a strange request. Can you clarify why you need this? Maybe there's a better solution.
     
  • Verified answer
    Aman Kakkar Profile Picture
    3,210 Super User 2026 Season 1 on at
    Thanks Yun Zhu and Valentin for your responses.
     
    I have edited my Question for better understanding. Also, I have got the solution for this. I used the below mentioned event (and code) and I was able to post some more lines along with purchase invoice, which acted the same way as the standard lines i.e. they were showing up in the Preview Posting -> Find Entries, as well as after Posting along with the Posted Document.
     
    Most important point - You have to take the "var GenJnlPostLine: codeunit "Gen. Jnl.-Post Line" " from the event only i.e. passing it using "var". If you create a new variable of this codeunit, you will fall into error while calling it via other functions.  
     
    EVENT - 
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Post Invoice Events", 'OnPostLedgerEntryOnBeforeGenJnlPostLine', '', false, false)]
        local procedure OnPostLedgerEntryOnBeforeGenJnlPostLine(var GenJnlLine: Record "Gen. Journal Line"; PurchHeader: Record "Purchase Header"; TotalPurchLine: Record "Purchase Line"; TotalPurchLineLCY: Record "Purchase Line"; PreviewMode: Boolean; SuppressCommit: Boolean; var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line")
        var
            PurchLine: Record "Purchase Line";
        begin
            if PurchHeader."Document Type" <> PurchHeader."Document Type"::Invoice then
                exit;
    
            PurchLine.Reset();
            PurchLine.SetRange("Document Type", PurchHeader."Document Type");
            PurchLine.SetRange("Document No.", PurchHeader."No.");
            PurchLine.SetRange(Type, PurchLine.Type::"G/L Account");
            if PurchLine.FindSet() then
                repeat
                    CreateAndPostGenJournalNegativeLine(PurchHeader, PurchLine, GenJnlPostLine, GenJnlLine);
                    CreateAndPostGenJournalPositiveLine(PurchHeader, PurchLine, GenJnlPostLine, GenJnlLine);
                until PurchLine.Next() = 0;
        end;
     
    For creating a reversal(negative) amount line:
    
        procedure CreateAndPostGenJournalReversalLine(PurchHeader: Record "Purchase Header"; PurchLine: Record "Purchase Line"; var GenJnlPostLine: codeunit "Gen. Jnl.-Post Line"; var VarGenJnlLine: Record "Gen. Journal Line")
        var
            ReversalJnlLine: Record "Gen. Journal Line";
        begin
            Setup.Get();
            Clear(ReversalJnlLine);
            ReversalJnlLine.Init();
            ReversalJnlLine."Journal Template Name" := Setup."Journal Template";
            ReversalJnlLine."Journal Batch Name" := Setup."Journal Batch";
            ReversalJnlLine."Line No." := GetNextLineNoForGenJnl(ReversalJnlLine."Journal Template Name", ReversalJnlLine."Journal Batch Name");
            ReversalJnlLine.Validate("Account Type", ReversalJnlLine."Account Type"::"G/L Account");
            ReversalJnlLine.Validate("Account No.", PurchLine."No.");
            ReversalJnlLine.Validate("Posting Date", PurchHeader."Posting Date");
            ReversalJnlLine.Validate("Document Type", ReversalJnlLine."Document Type"::" ");
            ReversalJnlLine.Validate("Document No.", VarGenJnlLine."Document No.");
            ReversalJnlLine.Validate(Amount, -PurchLine."Amount Including VAT");
            ReversalJnlLine.Validate("Dimension Set ID", PurchLine."Dimension Set ID");
            ReversalJnlLine.Validate("Gen. Posting Type", VarGenJnlLine."Gen. Posting Type");
            ReversalJnlLine.Validate("Gen. Prod. Posting Group", VarGenJnlLine."Gen. Prod. Posting Group");
            ReversalJnlLine.Validate("Gen. Prod. Posting Group", VarGenJnlLine."Gen. Prod. Posting Group");
            ReversalJnlLine."Is Reversal Line" := true;
            ReversalJnlLine.Insert();
            GenJnlPostLine.RunWithCheck(ReversalJnlLine);
        end;
     
     
    For creating a positive amount line (Just the Amount is taken as positive, else everything is same.
        procedure CreateAndPostGenJournalPositiveLine(PurchHeader: Record "Purchase Header"; PurchLine: Record "Purchase Line"; var GenJnlPostLine: codeunit "Gen. Jnl.-Post Line"; var VarGenJnlLine: Record "Gen. Journal Line")
        var
            ReversalJnlLine: Record "Gen. Journal Line";
        begin
            Setup.Get();
            Clear(ReversalJnlLine);
            ReversalJnlLine.Init();
            ReversalJnlLine."Journal Template Name" := Setup."Journal Template";
            ReversalJnlLine."Journal Batch Name" := Setup."Journal Batch";
            ReversalJnlLine."Line No." := GetNextLineNoForGenJnl(ReversalJnlLine."Journal Template Name", ReversalJnlLine."Journal Batch Name");
            ReversalJnlLine.Validate("Account Type", ReversalJnlLine."Account Type"::"G/L Account");
            ReversalJnlLine.Validate("Account No.", PurchLine."No.");
            ReversalJnlLine.Validate("Posting Date", PurchHeader."Posting Date");
            ReversalJnlLine.Validate("Document Type", ReversalJnlLine."Document Type"::" ");
            ReversalJnlLine.Validate("Document No.", VarGenJnlLine."Document No.");
            ReversalJnlLine.Validate(Amount, PurchLine."Amount Including VAT");
            ReversalJnlLine.Validate("Dimension Set ID", PurchLine."Dimension Set ID");
            ReversalJnlLine.Validate("Gen. Posting Type", VarGenJnlLine."Gen. Posting Type");
            ReversalJnlLine.Validate("Gen. Prod. Posting Group", VarGenJnlLine."Gen. Prod. Posting Group");
            ReversalJnlLine.Validate("Gen. Prod. Posting Group", VarGenJnlLine."Gen. Prod. Posting Group");
            ReversalJnlLine."Is Reversal Line" := true;
            ReversalJnlLine.Insert();
            GenJnlPostLine.RunWithCheck(ReversalJnlLine);
        end;
     
     

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 March Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,924 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,169 Super User 2026 Season 1

#3
Teagen Boll Profile Picture

Teagen Boll 734 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans