Sub topic 13
Tracing document links with ABAP SELECT statements
This chapter assumes you can read the document flow in the SAP GUI, know the header and item structure of SD, MM and FI documents, and can write basic ABAP SELECT statements with inline declarations. Earlier chapters covered VBFA, EKBE and the accounting reference fields. Here you combine them into queries that follow a chain by program.
The tracing idea
A document flow is a set of reference links stored in several tables. No single table holds the whole chain. A trace is a series of lookups, where the result of one query becomes the key of the next.
| Chain segment | Table | Key you start with | What you get |
|---|---|---|---|
| Sales order to delivery or invoice | VBFA | VBELV, POSNV | VBELN, POSNN, VBTYP_N |
| Purchase order to goods or invoice receipt | EKBE | EBELN, EBELP | VGABE, GJAHR, BELNR, BUZEI |
| Billing document to accounting document | BKPF | AWTYP, AWKEY | BUKRS, BELNR, GJAHR |
Step 1: Direct follow-on documents from VBFA
In VBFA, VBELV and POSNV identify the preceding document and item. VBELN, POSNN and VBTYP_N identify the subsequent document, item and category.
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 direct successors of the order. If a delivery was created and then billed, the billing document appears as a successor of the delivery. It may also appear as a successor of the order, depending on the process. To see the whole chain, you must repeat the query with each result as the new starting point.
The category in VBTYP_N tells you what each row is. Common values are J for delivery, M for invoice, O for credit memo, H for returns and R for goods movement. Do not hard-code guesses. Check the domain values of VBTYP in SE11 on your system.
Step 2: Walking the full chain
A loop that expands level by level works well, because chains are shallow, usually four to six levels. The program below collects every descendant of a starting document. It tracks visited documents so a repeated link cannot cause an endless loop.
REPORT z_trace_sales_flow.
PARAMETERS p_vbeln TYPE vbeln_va OBLIGATORY.
START-OF-SELECTION.
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,
END OF ty_link.
DATA: lt_all TYPE STANDARD TABLE OF ty_link WITH EMPTY KEY,
lt_level TYPE STANDARD TABLE OF ty_link WITH EMPTY KEY,
lt_frontier TYPE STANDARD TABLE OF vbfa-vbelv WITH EMPTY KEY,
lt_seen TYPE SORTED TABLE OF vbfa-vbelv WITH UNIQUE KEY table_line.
APPEND p_vbeln TO lt_frontier.
INSERT p_vbeln INTO TABLE lt_seen.
WHILE lt_frontier IS NOT INITIAL.
CLEAR lt_level.
SELECT vbelv, posnv, vbeln, posnn, vbtyp_n
FROM vbfa
FOR ALL ENTRIES IN @lt_frontier
WHERE vbelv = @lt_frontier-table_line
INTO TABLE @lt_level.
CLEAR lt_frontier.
LOOP AT lt_level INTO DATA(ls_link).
APPEND ls_link TO lt_all.
IF NOT line_exists( lt_seen[ table_line = ls_link-vbeln ] ).
INSERT ls_link-vbeln INTO TABLE lt_seen.
APPEND ls_link-vbeln TO lt_frontier.
ENDIF.
ENDLOOP.
ENDWHILE.
SORT lt_all BY vbelv posnv vbeln posnn.
DELETE ADJACENT DUPLICATES FROM lt_all COMPARING ALL FIELDS.
LOOP AT lt_all INTO ls_link.
WRITE: / ls_link-vbelv, ls_link-posnv, '->',
ls_link-vbeln, ls_link-posnn, ls_link-vbtyp_n.
ENDLOOP.Points to note:
FOR ALL ENTRIESremoves duplicate rows from the result. It also returns nothing useful if the driver table is empty, so the loop guard onlt_frontiermatters.- Select the key fields you need. VBFA has no unique-key guarantee in your selected column list, so deduplicate after the loop.
- Depth is bounded by real processes, but the visited-set protects you from bad data.
Step 3: Purchase order history from EKBE
EKBE stores one row per history event per PO item. VGABE separates goods receipts (1), invoice receipts (2) and subsequent debits or credits (3). BELNR and GJAHR identify the follow-on document.
DATA lv_po TYPE ebeln VALUE '4500000123'.
SELECT ebeln, ebelp, vgabe, gjahr, belnr, buzei, menge
FROM ekbe
WHERE ebeln = @lv_po
ORDER BY ebelp, vgabe, gjahr, belnr, buzei
INTO TABLE @DATA(lt_history).EKBE has no pointer to a "next" document. Each row is a leaf that hangs directly under the PO item. The same row can be a material document (goods receipt) or a supplier invoice document, so you must read VGABE before deciding which table BELNR belongs to. For VGABE 1, BELNR is a material document number and GJAHR its year. For VGABE 2, BELNR is an invoice document number.
Also look at the sign of the movement. Reversals appear as extra rows with a reversing indicator in the same history, not as deletions of the original row. The chapter on reversals covers this. For tracing, the practical rule is to sum quantities by sign before concluding that a PO item is fully received.
Step 4: From the source document to accounting
The accounting document is found from the other side. FI documents carry a reference to their source in the header table BKPF. AWTYP names the reference transaction and AWKEY holds the key of the source 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).For billing documents, AWKEY contains the billing document number. For invoice receipts in MM, the reference transaction is RMRP and AWKEY is built from the invoice document number and fiscal year. That key is longer than the bare document number, so do not compare it with a plain BELNR value. Check a real posted document in SE16N first to see how your system fills the field.
Because AWKEY is a character field, pad the number to the field length with leading zeros as shown. A missing zero returns an empty result without any error.
A common mistake is to run this query without BUKRS. Document numbers in BKPF are unique only within company code and fiscal year. When AWKEY comes from a billing document, the match is normally unique, but you should still carry BUKRS and GJAHR forward when you read the items from BSEG or the line item views.
Combining the segments
A full order-to-cash trace works in this sequence:
- Expand the sales chain from VBFA as in step 2.
- Keep the rows where
vbtyp_nindicates an invoice. Take those VBELN values. - Look up BKPF with
awtyp = 'VBRK'and each billing number. - Report the order, delivery, billing and accounting numbers together.
A procure-to-pay trace has fewer steps. Read EKBE for the PO, split by VGABE, and then use BKPF with the invoice reference to reach the accounting document.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Query returns no rows for a known order | Number not padded with leading zeros | Use the internal format, for example with the conversion exit ALPHA |
| Chain stops after one level | Query ran only once | Loop on the successors as in step 2 |
| Missing accounting document | Billing document not released to accounting | Check the billing document's posting status before blaming the query |
| Fewer rows than expected with `FOR ALL ENTRIES` | Duplicate rows removed from the result | Select all key fields, or use a join or subquery |
| Wrong quantity on PO item | Reversals and returns ignored | Respect the movement sign when summing |
Performance and safety
- Start from the key fields. VBFA is indexed by the preceding document, so
WHERE vbelv = ...is fast. A query by successor alone can be slow on large systems. - Never write a query that reads VBFA or BKPF without a restricting condition on a production system.
- These are read-only queries. Document links are maintained by the standard transactions. Do not insert or change rows in VBFA or EKBE yourself.
Exercise
You select from VBFA with WHERE vbelv = @lv_order and get a delivery, but not the billing document you see in the GUI flow. Why, and how do you reach it?
Show answer
The query returns only direct successors, and in this process the billing document follows the delivery rather than the order. Repeat the query with the delivery number as VBELV, or use the loop from step 2 to expand every level until no new documents appear.
Exercise
You have a billing document number and need its accounting document. Which table and fields do you query, and what do you check about the key?
Show answer
Query BKPF with awtyp = 'VBRK' and awkey equal to the billing document number, padded to the AWKEY field length with leading zeros. Check the result by company code and fiscal year, because accounting document numbers are unique only within those.