Business Central

Hacking Business Central posting preview

Jalmaraz Profile Picture Jalmaraz 667  | 

From event RTC versions we have posting preview feature. We have it in Sales Invoices, and other like orders, purchases, etc.:

It shows a post simulation, and you can navigate in virtual tables that show you how could be the real post and its final entries:

If you carefully see the screen, you see well-known entries G/L Entry, Cust. Ledger Entry, Item Ledger, but the last one Excise Tax Entry is new. Is a entry table from our company TIPSA  S.L., Excise Taxes Spain - IIEE alcohol y SILICIE app.

So, we get our own entry table in this preview, and spoiler, it is no easy. It is no easy because we can´t directly modify base app, and required to study posting preview, and use some tech, that not everyone handle very often.

Workshop: including your own entry table in posting preview

The first step is creating a new codeunit with this property:
codeunit 69000 "TIP IIEE Post Preview"
    SingleInstance = true;
The SingleInstance property is described in this MS learn entry: SingleInstance Property - Business Central | Microsoft Learn , but the idea of a single instance of an object is to save his state through user session, so their global vars will save their value until session closing or value explicit clearing. This is the key, because we declare these globals:
        TempExciseTaxEntry: Record "TIP Excise Tax Entry" temporary;
        IsPreview: Boolean;
First subscription: When preview post begin we load Boolean switch and delete temporary table:
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Preview", 'OnAfterBindSubscription', '', false, false)]
    local procedure ActivePreview(var PostingPreviewEventHandler: Codeunit "Posting Preview Event Handler")
        IsPreview := true;
While preview posting if some entry is created, we write the same records in our temporary table:
    [EventSubscriber(ObjectType::Table, Database::"TIP Excise Tax Entry", 'OnAfterInsertEvent', '', false, false)]
    local procedure OnAfterInsertExciseTax(var Rec: Record "TIP Excise Tax Entry")
        if not IsPreview then
        if rec.IsTemporary() then
        TempExciseTaxEntry := Rec;
        if TempExciseTaxEntry.Insert() then;
On preview post end we deactivate switch for avoid more temporary record saving:
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Preview", 'OnAfterUnbindSubscription', '', false, false)]
    local procedure DeActivePreview()
        IsPreview := false;
Before raise the navigate page we dump into document entry table our temporary table:
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Posting Preview Event Handler", 'OnAfterFillDocumentEntry', '', false, false)]
    local procedure ShowAllEntries(var DocumentEntry: Record "Document Entry" temporary)
        PostingPreviewEventHandler: Codeunit "Posting Preview Event Handler";
        if not TempExciseTaxEntry.IsEmpty then
            PostingPreviewEventHandler.InsertDocumentEntry(TempExciseTaxEntry, DocumentEntry);
And last subscription. If user click Show related Entries in our own entry table we put this subscription to show the temp table records in a its page:
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Posting Preview Event Handler", 'OnAfterShowEntries', '', false, false)]
    local procedure ShowIIEEEntries(TableNo: Integer)
        if TableNo <> Database::"TIP Excise Tax Entry" then
        Page.Run(Page::"TIP Excise Tax Entries", TempExciseTaxEntry);

Hope it is useful. You can find all the code in the repository:
JalmarazMartn/PostPreview (


Related stories