Sub topic 5
Where sales links are stored: table VBFA
This chapter assumes you can open SE16 or SE16N, read a header and item structure, and write a basic ABAP SELECT. Here you learn how table VBFA stores the links you saw in the document flow display, and how to read them.
What VBFA holds
VBFA is the sales document flow table. Each row is one link between a preceding document and a subsequent document. The GUI document flow (transaction VA03, "Display Document Flow") is built largely from these rows.
Each row names both ends of the link:
| Field | Meaning |
|---|---|
| VBELV | Preceding document number |
| POSNV | Preceding item number |
| VBTYP_V | Category of the preceding document |
| VBELN | Subsequent document number |
| POSNN | Subsequent item number |
| VBTYP_N | Category of the subsequent document |
| RFMNG | Quantity referenced in the base unit |
| ERDAT | Date the link was created |
A link is stored at item level. An order with three items that is fully delivered produces at least three rows, one for each item pair. The header-level link uses item number 000000.
Reading links forward
To find what was created from a sales order, filter on the preceding document. The query below returns the direct follow-on documents.
DATA lv_order TYPE vbeln_va VALUE '0000004711'.
SELECT vbelv, posnv, vbeln, posnn, vbtyp_n
FROM vbfa
WHERE vbelv = @lv_order
INTO TABLE @DATA(lt_followers).The result holds only direct successors. If the order led to a delivery and the delivery led to an invoice, the invoice does not appear here. Its row has the delivery as VBELV. To reach it, run the query again with each delivery number.
Reading links backward
To find where a document came from, filter on VBELN instead. For an invoice, this returns the delivery items it references.
DATA lv_invoice TYPE vbeln_vf VALUE '0090000123'.
SELECT vbelv, posnv, vbtyp_v, vbeln, posnn
FROM vbfa
WHERE vbeln = @lv_invoice
INTO TABLE @DATA(lt_predecessors).Forward and backward reads use different fields, so the performance differs. VBFA has a primary key starting with VBELV, so forward reads are efficient. Backward reads by VBELN depend on a secondary index. Check in SE11 that one exists in your system before running backward reads on large volumes.
Using the category fields
VBTYPV and VBTYPN hold document categories, which the previous chapter introduced. They let you filter a chain by document kind without reading the documents themselves.
| Code | Document |
|---|---|
| C | Sales order |
| J | Delivery |
| M | Invoice |
| H | Returns |
| R | Goods movement (material document) |
A forward read restricted to deliveries looks like this:
SELECT vbeln, posnn, rfmng
FROM vbfa
WHERE vbelv = '0000004711'
AND vbtyp_n = 'J'
INTO TABLE @DATA(lt_deliveries).What VBFA does not cover
VBFA records links among sales and distribution documents, including the goods movement posted for a delivery. It does not hold purchasing history, which lives in EKBE. It also does not record the accounting document for an invoice. That link is stored on the accounting side, in the reference fields of BKPF. Later chapters cover both.
Common errors
- Reading only the header row. Item-level quantities and partial deliveries are visible only when you read POSNV and POSNN.
- Assuming one hop is the whole chain. VBFA stores direct links only. Tracing a full chain means repeating the read.
- Mixing up number domains. A VBELN value can be a delivery, an invoice or a material document. Use the category field to tell them apart.
- Ignoring leading zeros. Document numbers are stored with leading zeros. A literal such as
'4711'in aWHEREclause on a character field does not match'0000004711'.
Exercise
A sales order 4711 was delivered and the delivery was invoiced. You run a SELECT on VBFA with VBELV = '0000004711'. Why is the invoice not in the result, and how do you find it?
Show answer
VBFA stores only direct links. The invoice row has the delivery as VBELV, not the order. Take the delivery number from the first result, then query VBFA again with VBELV set to that delivery number to get the invoice.