Sub topic 15
Tracing a transaction through document flow
This chapter assumes you have read the earlier chapters on the sales order, delivery, goods issue and billing. It also assumes you can read basic SQL and ABAP SELECT statements. You should know that each of those steps creates its own document. Here you will learn how SAP links those documents, how to read the links, and where the links stop.
What document flow is
Document flow is the chain of links between the documents of one business transaction. When you create a delivery with reference to a sales order, SAP writes a link record. The same happens for the goods movement, the invoice and the credit memo. In the SAP GUI, open the order in VA03 and choose the document flow button. The screen shows a tree of the sales order and everything that followed it.
The links live in table VBFA (sales document flow). Each row connects one predecessor to one successor:
| Field | Meaning |
|---|---|
| VBELV, POSNV | Predecessor document number and item |
| VBTYP_V | Predecessor document category |
| VBELN, POSNN | Successor document number and item |
| VBTYP_N | Successor document category |
| RFMNG, MEINS | Quantity that the successor took over from the predecessor |
VBFA stores only direct links. A sales order points to its delivery, and the delivery points to the invoice. There is no row that points from the order straight to the invoice. To reconstruct the whole chain, you follow the links one step at a time.
Document categories
The categories in VBTYPV and VBTYPN tell you what kind of document each number is. These are the ones you need for order-to-cash:
| Category | Document |
|---|---|
| A | Inquiry |
| B | Quotation |
| C | Sales order |
| J | Outbound delivery |
| R | Goods movement (material document) |
| M | Invoice |
| N | Invoice cancellation |
| O | Credit memo |
| H | Returns order |
| K | Credit memo request |
Always read the category together with the number. A material document, a delivery and an invoice live in different number ranges, but the same number can occur in more than one of them.
The last two boxes are not VBFA links. Section "Crossing into accounting" explains why.
Reading one level of the flow
This query lists the direct successors of one sales order:
DATA(lv_vbeln) = CONV vbeln_va( '0000012345' ).
SELECT vbelv, posnv, vbeln, posnn, vbtyp_n
FROM vbfa
WHERE vbelv = @lv_vbeln
INTO TABLE @DATA(lt_flow).Expect one row per order item and successor. If the order has three items and all were delivered, you get three rows for the delivery. If one item was delivered in two partial deliveries, that item appears twice with different VBELN values. The quantity in RFMNG tells you how much each delivery took.
Walking the whole chain
The following report follows the links forward level by level. It keeps the document number and category together, so a material document cannot be confused with an invoice that happens to share its number. The lv_level < 10 condition is a safety limit.
REPORT zo2c_flow_trace.
PARAMETERS p_vbeln TYPE vbeln_va OBLIGATORY.
TYPES: BEGIN OF ty_doc,
vbeln TYPE vbfa-vbeln,
vbtyp TYPE vbfa-vbtyp_n,
END OF ty_doc.
DATA lt_frontier TYPE STANDARD TABLE OF ty_doc WITH EMPTY KEY.
DATA lt_seen TYPE SORTED TABLE OF ty_doc WITH UNIQUE KEY vbeln vbtyp.
DATA lv_level TYPE i.
INSERT VALUE #( vbeln = p_vbeln vbtyp = 'C' ) INTO TABLE lt_frontier.
WHILE lt_frontier IS NOT INITIAL AND lv_level < 10.
lv_level += 1.
SELECT DISTINCT vbeln, vbtyp_n
FROM vbfa
FOR ALL ENTRIES IN @lt_frontier
WHERE vbelv = @lt_frontier-vbeln
AND vbtyp_v = @lt_frontier-vbtyp
INTO TABLE @DATA(lt_next).
CLEAR lt_frontier.
LOOP AT lt_next INTO DATA(ls_next).
DATA(ls_doc) = VALUE ty_doc( vbeln = ls_next-vbeln vbtyp = ls_next-vbtyp_n ).
IF line_exists( lt_seen[ vbeln = ls_doc-vbeln vbtyp = ls_doc-vbtyp ] ).
CONTINUE.
ENDIF.
INSERT ls_doc INTO TABLE lt_seen.
INSERT ls_doc INTO TABLE lt_frontier.
WRITE: / lv_level, ls_doc-vbtyp, ls_doc-vbeln.
ENDLOOP.
ENDWHILE.For a fully processed order, the output lists the delivery (J), then in the next level the goods movement (R) and the invoice (M). Level numbers show the distance from the order. The lt_seen table prevents the same document from being printed twice when two items lead to it.
Collective billing makes the shape of the flow a graph. One invoice can contain several deliveries, and one order can lead to several invoices. The loop handles this because it tracks documents, not a single path.
Crossing into accounting
Billing is where SD hands over to FI. The invoice has an accounting document, and in S/4HANA its lines are stored in ACDOCA, the Universal Journal. The link is made by the reference fields on the journal line. For a billing document, the reference type is VBRK and the reference number is the billing document number:
DATA(lv_billing) = CONV vbeln_vf( '0090001234' ).
SELECT rbukrs, gjahr, belnr, docln, racct, hsl
FROM acdoca
WHERE rldnr = '0L'
AND awtyp = 'VBRK'
AND awref = @lv_billing
INTO TABLE @DATA(lt_journal).Expect at least a receivable line, revenue lines, and tax lines where tax applies. Goods issue works the same way, except that its reference is the material document, so you filter on the material document's reference type instead of VBRK.
Payments are different. A customer payment is created in FI, not in SD, so it has no VBFA row. To find it, take the accounting document of the invoice and look at its clearing document (field AUGBL in ACDOCA). If that field is empty, the receivable is still open. The chain from sales order to cash therefore has two parts: VBFA for the SD side, and the accounting document with its clearing information for the FI side.
Common errors
- Querying the wrong direction.
WHERE vbelv = ...finds successors.WHERE vbeln = ...finds predecessors. Swapping them returns an empty result and looks like a missing document. - Ignoring the category. Joining only on the document number can match documents of different types. Filter on VBTYPV and VBTYPN as well.
- Expecting a link from order to invoice. VBFA stores direct links only. You need the delivery in between, or a recursive walk like the one above. Orders without deliveries, such as service orders billed directly, link straight to the invoice.
- Looking for payments in VBFA. Payments and clearing belong to FI. Use the clearing fields of the accounting document.
- Forgetting cancellations. A cancelled invoice produces a new document with category N and a reversing accounting document. The original invoice stays in the flow, so check for both before concluding that billing is complete.
- Treating item numbers as stable. Item numbers in successor documents can differ from the order. Always carry POSNV and POSNN when you work at item level.
Exercise
A sales order 0000012345 has one item, delivered in two partial deliveries. Both deliveries were billed together in one collective invoice. How many successor rows does the order's item have in VBFA with category J, and which category leads from the deliveries to the invoice? Why does the walker report above still print the invoice only once?
Show answer
The order item has two VBFA rows with VBTYPN = 'J', one per partial delivery. Each delivery has a row pointing to the same invoice with VBTYPN = 'M'. The walker reaches the invoice from both deliveries. The second time, the pair (invoice number, category M) is already in the sorted table ltseen, so the lineexists check skips it and the invoice is printed once.