Sub topic 9
How accounting documents point back to their source
This chapter explains how a posted accounting document in FI records which logistics document created it. We use that link to move in both directions between SD or MM documents and FI.
This chapter assumes you know the following:
- You can read a table in SE16N.
- You know what a billing document, material document and invoice receipt are.
- You can read a basic ABAP
SELECT.
The sales and purchasing links (VBFA and EKBE) are covered in earlier chapters. Here we cover the third kind of link, which is stored in the accounting header table BKPF.
Why accounting needs its own link
VBFA stores links between sales and distribution documents. EKBE stores the history of a purchase order. Accounting documents belong to neither table. An FI document has its own key: company code, document number and fiscal year. It is often created automatically by a logistics transaction, and the system must remember who created it.
The accounting document therefore carries a pointer back to its origin. The pointer is a pair of header fields in BKPF. One field names the kind of source, and the other holds the source's key.
The reference fields in BKPF
The key of a BKPF row is BUKRS, BELNR and GJAHR. The following fields describe the origin.
| Field | Meaning |
|---|---|
| `AWTYP` | Reference procedure. It names the kind of source document. |
| `AWKEY` | Reference key. It holds the source document's key as text, up to 20 characters. |
| `AWSYS` | Logical system where the source document lives. |
| `XBLNR` | Reference document number. Users type it, for example a vendor's invoice number. |
| `BKTXT` | Document header text, also free text. |
AWTYP and AWKEY are the technical link. XBLNR and BKTXT are for people. They can be empty, duplicated or wrong, so never use them to build a reliable trace.
AWKEY is a single character field. Its layout depends on AWTYP, because it concatenates the parts of the source key.
| AWTYP | Source | AWKEY layout |
|---|---|---|
| `VBRK` | Billing document (SD) | Billing document number, 10 characters |
| `MKPF` | Material document (MM) | Material document number (10) + material document year (4) |
| `RMRP` | Invoice receipt (MM invoice verification) | Invoice document number (10) + fiscal year (4) |
| `BKPFF` | Posting made directly in FI | Document number (10) + company code (4) + fiscal year (4) |
Always check the layout in your own system before you rely on it. You can do this by opening a known document in FB03 and comparing its header reference with the SE16N row.
Order-to-cash: from billing to FI
When you release a billing document to accounting, SD creates the FI document. The system writes two links:
- In BKPF:
AWTYP = 'VBRK'andAWKEYholds the billing document number. - In VBRK: the field
BELNRholds the number of the accounting document.
This gives you a two-way link. You can start from either side.
This query finds the accounting document for a known billing document:
DATA lv_awkey TYPE awkey VALUE '0090000123'.
SELECT bukrs, belnr, gjahr, awtyp, awkey
FROM bkpf
WHERE awtyp = 'VBRK'
AND awkey = @lv_awkey
INTO TABLE @DATA(lt_fi_docs).The result can contain more than one row. A billing document that was cancelled has a second accounting document for the cancellation, and that document has its own reference. Read the rows with that in mind. The chapter on reversals covers how to tell them apart.
The billing document is the last SD step in the chain. The delivery's goods issue also posts to FI, and that posting has a different origin: the material document.
Procure-to-pay: two sources of FI documents
The purchasing chain produces accounting documents at two points:
- Goods receipt. The material document (MKPF) triggers an FI posting, for example stock against the goods receipt/invoice receipt clearing account. BKPF holds
AWTYP = 'MKPF'. - Invoice receipt. MM invoice verification posts the vendor liability. BKPF holds
AWTYP = 'RMRP'.
The key must be built from two parts. The code below finds the accounting document for a goods receipt:
DATA lv_mblnr TYPE mblnr VALUE '5000000321'.
DATA lv_mjahr TYPE mjahr VALUE '2026'.
DATA(lv_key) = CONV awkey( |{ lv_mblnr }{ lv_mjahr }| ).
SELECT bukrs, belnr, gjahr, awtyp, awkey
FROM bkpf
WHERE awtyp = 'MKPF'
AND awkey = @lv_key
INTO TABLE @DATA(lt_gr_fi).The same idea works for invoices: use AWTYP = 'RMRP' and concatenate the invoice document number with its fiscal year.
In EKBE, the PO history row for a goods receipt or invoice receipt carries BELNR and GJAHR. These are the material document or invoice document keys. They are not the accounting document keys. That is the usual starting point for a PO trace: read EKBE, build AWKEY from BELNR and GJAHR, then read BKPF.
Tracing backward: from FI document to source
Auditors and FI users usually start from the accounting document and need to find its cause. In FB03 you can use the document header's reference and the "Display document" or "Follow-on documents" functions. In code, read the reference pair and branch on AWTYP:
REPORT z_fi_source_lookup.
PARAMETERS:
p_bukrs TYPE bukrs OBLIGATORY,
p_belnr TYPE belnr_d OBLIGATORY,
p_gjahr TYPE gjahr OBLIGATORY.
START-OF-SELECTION.
SELECT SINGLE awtyp, awkey
FROM bkpf
WHERE bukrs = @p_bukrs
AND belnr = @p_belnr
AND gjahr = @p_gjahr
INTO @DATA(ls_ref).
IF sy-subrc <> 0.
WRITE / 'Accounting document not found.'.
RETURN.
ENDIF.
CASE ls_ref-awtyp.
WHEN 'VBRK'.
WRITE: / 'Billing document', ls_ref-awkey(10).
WHEN 'MKPF'.
WRITE: / 'Material document', ls_ref-awkey(10),
'year', ls_ref-awkey+10(4).
WHEN 'RMRP'.
WRITE: / 'Invoice document', ls_ref-awkey(10),
'year', ls_ref-awkey+10(4).
WHEN 'BKPFF' OR space.
WRITE / 'Posted directly in FI, no logistics source.'.
WHEN OTHERS.
WRITE: / 'Other source:', ls_ref-awtyp, ls_ref-awkey.
ENDCASE.The WHEN OTHERS branch matters. Other applications, such as asset accounting or travel management, set their own AWTYP values. Do not assume that every row maps to SD or MM.
Line-item references
The header says which document created the posting. The line items add more detail. In the classic BSEG table, an item can hold VBELN (sales document), EBELN and EBELP (purchase order and item), and VBEL2 and POSN2 for order-item references. These fields tell you which order or PO item a given line belongs to. A single billing document can post items that relate to many orders, so the header link alone does not give you the item-level story.
Common mistakes
- Searching by
XBLNR. It is user-entered text and not unique. UseAWTYPandAWKEY. - Forgetting the year. A material document number repeats each year. Without
MJAHRinAWKEYyou get the wrong row or none. - Hand-padding the key. Build
AWKEYwith string templates and check the result in SE16N. A shifted position gives an empty result with no error message. - Expecting one accounting document per source. Cancellations, reversals and multiple valuation areas can produce several rows for the same source.
- Missing index support.
AWTYPplusAWKEYis the selective combination. Filtering onAWKEYalone on a large BKPF can be slow.
A note on SAP S/4HANA
In SAP S/4HANA the universal journal table ACDOCA holds line items, and it carries its own reference fields. BKPF still exists as the header. The final chapter of this tutorial covers what changes. The principle in this chapter stays the same: the accounting document keeps a pointer to its source.
Exercise
A material document 5000000321 from fiscal year 2026 posted a goods receipt for a purchase order. How do you find the matching accounting document in BKPF, and why must the year be part of the search?
Show answer
Build the reference key by concatenating the material document number and the year: '50000003212026'. Select from BKPF where AWTYP = 'MKPF' and AWKEY equals that value. The year is needed because material document numbers restart each year, so the number alone can match documents from different years.
Exercise
You have accounting document 1900000045 in company code 1000, year 2026, and BKPF shows AWTYP = 'VBRK' and AWKEY = '0090000123'. Name the source document and one way to confirm the link from the other side.
Show answer
The source is billing document 0090000123. To confirm, read VBRK for that billing document and check that its field BELNR holds 1900000045, or open the document flow of the billing document and follow the accounting document link.