Mapping from Business Central documents to ZUGFeRD and XRechnung

Fornav Knowledge Base — updated with a worked customization example and Claude Code skill guidance.

Fornav out of the box contains mappings from Business Central documents to ZUGFeRD and XRechnung, which cover populating the mandatory fields in the standard defined by the Coordination Office for IT Standards (KoSIT) (see ForNAV Support for ZUGFeRD-installation guide-Final.pdf).

However, in some cases you might want to add information that is not mandatory.

To do this, you can modify the default mapping (eSalesInvoice.Codeunit.al or eSalesCreditNote.Codeunit.al) by overriding the event in the “ForNAV eDocument Interface”:

The “OnAfterDocument2InvoiceDescriptor” event is used to change the FORNAV default mapping and the “OnDocument2InvoiceDescriptor” event is used to override the default mapping completely and to map new document types.


codeunit 6189213 "ForNAV eDocument Interface"
{
    [IntegrationEvent(false, false)]
    internal procedure OnAfterDocument2InvoiceDescriptor(var InvoiceDescriptor: Record "ForNAV InvoiceDescriptor")
    begin
    end;
 
    [IntegrationEvent(false, false)]
    internal procedure OnDocument2InvoiceDescriptor(var InvoiceDescriptor: Record "ForNAV InvoiceDescriptor"; var eInvoice: Record "ForNAV eInvoice")
    begin
    end;
}

Example: Adding a custom note via the OnAfterDocument2InvoiceDescriptor event

The OnAfterDocument2InvoiceDescriptor event fires after the built-in mapping codeunit (eSalesInvoice, eSalesCreditNote, eServiceInvoice, eServiceCreditNote, ePurchaseInvoice, or ePurchaseCreditNote) has fully populated the InvoiceDescriptor from the posted document, and before it is serialized to XML — the right point to add or correct a field without having to reimplement the whole mapping.

The following example subscribes from a separate extension and adds a header note (BT-22) built from the customer’s “Your Reference” on the originating Sales Invoice. Two things to note:

Child records such as notes, tax lines, and trade allowance charges are virtualized, so always populate them via InvoiceDescriptor.InitXxx and persist them via Codeunit “ForNAV eDocument”’s Insert/Modify procedures. Do not use the temporary record’s own .Insert().

InvoiceDescriptor.Type only narrows the document down to “Invoice” or “CreditNote”. A Sales Invoice and a Service Invoice are both set to Type::Invoice identically, so distinguishing between them means Get()-ing the specific source table and treating failure as “not this document type”.


[EventSubscriber(ObjectType::Codeunit, Codeunit::"ForNAV eDocument Interface", 'OnAfterDocument2InvoiceDescriptor', '', false, false)]
local procedure OnAfterDocument2InvoiceDescriptor(var InvoiceDescriptor: Record "ForNAV InvoiceDescriptor")
var
    SalesInvoiceHeader: Record "Sales Invoice Header";
    TempNote: Record "ForNAV Note" temporary;
    eDocument: Codeunit "ForNAV eDocument";
begin
    if InvoiceDescriptor.Type <> InvoiceDescriptor.Type::Invoice then
        exit;
    if not SalesInvoiceHeader.Get(InvoiceDescriptor.InvoiceNo) then
        exit;
 
    InvoiceDescriptor.InitNotes(TempNote);
    TempNote.Content := CopyStr(SalesInvoiceHeader."Your Reference", 1, MaxStrLen(TempNote.Content));
    TempNote.SubjectCode := TempNote.SubjectCode::AAI;
    eDocument.Insert(TempNote);
end;
 

ZUGFeRD support for non-Fornav reports

To add ZUGFeRD support for non-Fornav reports, you simply subscribe to an event in the report that is called with the document header record, and then call the AddZugFeRD method in the “FORNAV eDocument Interface” codeunit with the report number and the RecordId:


[EventSubscriber(ObjectType::Report, Report::"Standard Sales - Invoice", 'OnAfterGetSalesHeader', '', true, true)]
local procedure OnAfterGetSalesInvoiceHeader(SalesInvoiceHeader: Record "Sales Invoice Header")
var
    eDocument: Codeunit "ForNAV eDocument Interface";
begin
    eDocument.AddZugFeRD(Report::"Standard Sales - Invoice", SalesInvoiceHeader.RecordId);
end;
 

Using Claude Code to build ZUGFeRD customizations

Fornav publishes a Claude Code skill that guides you through writing customizations like the one described previously — it knows the internalsVisibleTo requirement, the InvoiceDescriptor object model, and the difference between the OnAfterDocument2InvoiceDescriptor and OnDocument2InvoiceDescriptor events. It also verifies its suggestions against the actual ZUGFeRD source, rather than relying on assumptions.

Installing the skill

To install the skill, run the following commands once in any Claude Code session (desktop app, VS Code extension, or CLI):


/plugin marketplace add https://github.com/fornav/ForNav.Claude
/plugin install fornav-al-reports@fornav-claude-plugins
 

The installation is per machine and the plugin persists across future sessions after it is installed.

Applying the skill

The skill auto-triggers when you ask Claude to customize ZUGFeRD/XRechnung output, so a natural request is usually enough, for example:

  • “Add a custom note to the ZUGFeRD XML for Sales Invoices using OnAfterDocument2InvoiceDescriptor”
  • “Why won’t my subscriber to OnAfterDocument2InvoiceDescriptor compile?”
  • “How do I add a custom trade allowance charge line to a Service Credit Memo’s e-invoice XML?”

You can also invoke it directly using /fornav-al-reports:fornav-zugferd-customization.

What it covers:

  • The internalsVisibleTo gate on OnAfterDocument2InvoiceDescriptor — the most common reason a subscriber fails to compile, and why the fix is on ZUGFeRD’s side, not the subscriber’s.
  • Choosing between OnAfterDocument2InvoiceDescriptor (additive customization) and OnDocument2InvoiceDescriptor (full mapping replacement).
  • The InvoiceDescriptor child-record object model (InitXxx/FindFirstXxx), and why a child record’s own .Insert() only persists on Business Central 22 and later.
  • Why .Type and .Name can’t distinguish sibling document types (for example, a Sales Credit Memo from a Service Credit Memo), and what to check instead.
  • When to fetch the current ZUGFeRD specification from FeRD instead of relying on potentially outdated assumptions about BT-xxx business terms.
  • Testing guidance — compiling against ZUGFeRD’s symbols and exercising the real print/attach path, rather than only inspecting InvoiceDescriptor in isolation.

Related documents

ForNav.Claude — Claude Code plugin marketplace

ForNAV Support for ZUGFeRD – installation guide

eSalesInvoice.Codeunit.al

eSalesCreditNote.Codeunit.al