Sub topic 5
Where sales links are stored: table VBFA
This chapter assumes you can read tables in SE16 or SE16N. It also assumes you know the header and item structure of sales documents and basic ABAP SELECT syntax. Earlier chapters covered the idea of document flow and the document categories. Here you look at the table that stores the links for sales and distribution (SD) documents.
What VBFA is
VBFA is the sales document flow table. Each row is one link between a preceding document item and a subsequent document item. It does not hold business data such as prices or partners. It holds only the reference, the category of the follow-on document, and some quantity and value information.
The same table covers all SD document types: inquiries, quotations, orders, deliveries, invoices, returns and credit memo requests. This is why one query can walk the whole chain.
The fields that matter
| Field | Meaning |
|---|---|
| `VBELV` | Preceding document number |
| `POSNV` | Preceding item number (`000000` for a header-level link) |
| `VBTYP_V` | Category of the preceding document |
| `VBELN` | Subsequent document number |
| `POSNN` | Subsequent item number |
| `VBTYP_N` | Category of the subsequent document |
| `RFMNG` | Referenced quantity, in the base unit given by `MEINS` |
| `RFWRT` | Referenced value, in the currency in `WAERS` |
| `PLMIN` | Sign: `+` or `-`, showing whether the follow-on increases or reduces the quantity the predecessor still has open |
| `MJAHR` | Fiscal year, filled when the subsequent document is a material document |
| `ERDAT`, `ERZET` | Creation date and time of the link |
Read each row as "document VBELV item POSNV was followed by document VBELN item POSNN, which is of category VBTYP_N". The category is stored on the follow-on side. To know what kind of document VBELN is, read VBTYP_N. To know what VBELV is, read VBTYP_V.
Some category codes you will see most often:
| Code | Document |
|---|---|
| `C` | Sales order |
| `H` | Returns |
| `J` | Delivery |
| `M` | Invoice |
| `N` | Invoice cancellation |
| `O` | Credit memo |
| `R` | Goods movement (material document) |
One row per link, not per document
A document with several items produces several rows. A delivery for a two-item order produces at least two rows. Posting the goods issue adds more rows, because the delivery is followed by a material document. Item-level rows are what make partial deliveries visible.
Each arrow is one row in VBFA. In this example, the invoice is linked to the delivery item, not directly to the order item. That depends on the billing relevance of the order type (order-related or delivery-related billing). VBFA records the reference that was actually used.
Some rows exist only as a byproduct of how the system works. For example, the document flow can also contain rows for a document and the same document at a different level. Never assume that the number of rows equals the number of documents.
Reading forward from a known document
The first query finds the direct follow-on documents of one order. VBELV and POSNV identify the preceding document and item. VBELN, POSNN and VBTYP_N identify what came after.
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).This returns only the first level. A delivery appears, but the invoice created from that delivery does not, because its VBELV is the delivery number. To see the whole chain, repeat the query with each new VBELN as the next VBELV.
Reading backward from a known document
To find where a document came from, swap the roles of the fields. Filter on VBELN and read VBELV, POSNV and VBTYP_V:
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).Backward reads depend on a secondary index on the subsequent document fields. Check the indexes of VBFA in SE11 on your system before you run backward reads against large volumes.
Walking the whole chain
The next report starts at a sales document and follows VBELV to VBELN level by level. It stops after ten levels, which is more than any normal SD chain, and it stops when a level returns nothing.
REPORT zdemo_vbfa_chain.
PARAMETERS p_vbeln TYPE vbeln_va OBLIGATORY.
TYPES: BEGIN OF ty_link,
vbelv TYPE vbfa-vbelv,
posnv TYPE vbfa-posnv,
vbeln TYPE vbfa-vbeln,
posnn TYPE vbfa-posnn,
vbtyp_n TYPE vbfa-vbtyp_n,
rfmng TYPE vbfa-rfmng,
meins TYPE vbfa-meins,
END OF ty_link.
START-OF-SELECTION.
DATA lt_open TYPE STANDARD TABLE OF vbfa-vbelv WITH EMPTY KEY.
DATA lt_new TYPE STANDARD TABLE OF ty_link WITH EMPTY KEY.
DATA lt_all TYPE STANDARD TABLE OF ty_link WITH EMPTY KEY.
lt_open = VALUE #( ( p_vbeln ) ).
DO 10 TIMES.
IF lt_open IS INITIAL.
EXIT.
ENDIF.
SELECT vbelv, posnv, vbeln, posnn, vbtyp_n, rfmng, meins
FROM vbfa
FOR ALL ENTRIES IN @lt_open
WHERE vbelv = @lt_open-table_line
INTO TABLE @lt_new.
APPEND LINES OF lt_new TO lt_all.
CLEAR lt_open.
LOOP AT lt_new INTO DATA(ls_new).
" Material documents do not continue the SD chain.
IF ls_new-vbtyp_n = 'R'.
CONTINUE.
ENDIF.
IF NOT line_exists( lt_open[ table_line = ls_new-vbeln ] ).
APPEND ls_new-vbeln TO lt_open.
ENDIF.
ENDLOOP.
ENDDO.
LOOP AT lt_all INTO DATA(ls_all).
WRITE: / ls_all-vbelv, ls_all-posnv, '->',
ls_all-vbeln, ls_all-posnn, ls_all-vbtyp_n,
ls_all-rfmng UNIT ls_all-meins, ls_all-meins.
ENDLOOP.Two details matter here. The IF lt_open IS INITIAL guard is required, because FOR ALL ENTRIES with an empty driver table reads every row of the table. The 'R' check keeps the report from treating a material document number as a sales document number. Number ranges of different document types can overlap, and VBFA does not make the numbers unique across types.
The accounting document is not a normal VBFA link
Do not expect the financial accounting (FI) document in VBFA. The accounting document references its source through the reference fields of BKPF, not through a VBFA row. For a billing document, the reference transaction is AWTYP = 'VBRK' and the reference key AWKEY holds the billing document number. A later chapter covers this in detail. For now, remember that the SD chain in VBFA ends at the billing document, and the jump to FI uses a different table.
Common errors
- Forgetting leading zeros.
VBELVandVBELNare stored with leading zeros. In SE16 the conversion exit usually hides this, but in aSELECTyou must use a correctly typed variable or pad the literal. - Reading only the first level. One query on
VBELVreturns direct successors only. - Ignoring
POSNVandPOSNN. A header-level filter onVBELVmixes all items. For partial processing, filter by item too. - Summing
RFMNGwithout looking atVBTYP_NandPLMIN. Quantities from deliveries, returns and cancellations point in different directions. Add them up only after you decide which categories belong in the total. - Assuming VBFA holds all documents in the business process. Purchasing history lives in EKBE, and FI documents are found through BKPF references.
Exercise
Order 4711 has item 10 for 100 pieces. Two deliveries of 40 and 60 pieces were created for it. How many VBFA rows with VBTYP_N = 'J' do you expect for the item, and which fields do you read to get the quantity delivered?
Show answer
Two rows, one per delivery, both with VBELV = 4711, POSNV = 000010 and VBTYP_N = 'J'. The quantity is in RFMNG (40 and 60, in the unit MEINS). Adding both gives 100.
Exercise
A report that selects from VBFA with FOR ALL ENTRIES returns the entire table on some days. What is the likely cause, and how do you prevent it?
Show answer
The driver table was empty. FOR ALL ENTRIES ignores the WHERE condition on the driver field when the driver table is empty and reads all rows. Check that the driver table is not initial before the SELECT, as the chain report does with IF lt_open IS INITIAL.