Sub topic 13
Tracing document links with ABAP SELECT statements
This chapter assumes you can read SAP tables in SE16N, know the SD, MM and FI document structure, and can write a basic ABAP SELECT. You will query the three link stores covered earlier: VBFA for sales documents, EKBE for purchasing history, and the BKPF reference fields for accounting.
The working model
Each module keeps its links in a different place. A trace is a sequence of queries where the output keys of one query feed the next.
| Chain step | Table | Link fields |
|---|---|---|
| Sales document to follow-on | VBFA | VBELV, POSNV to VBELN, POSNN, VBTYP_N |
| Purchase order to GR and IR | EKBE | EBELN, EBELP, VGABE, BELNR, GJAHR |
| Billing document to FI | BKPF | AWTYP, AWKEY |
Sales links from VBFA
VBFA stores one row per link. VBELV and POSNV hold the preceding document and item. VBELN, POSNN and VBTYP_N hold 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 direct followers only. To go further, run the query again with each follower's vbeln as the new vbelv.
Purchase history from EKBE
EKBE holds the history of a purchase order item. VGABE tells you the kind of event: 1 is a goods receipt, 2 is an invoice receipt, and 3 is a subsequent debit or credit.
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).Always keep GJAHR with BELNR. Document numbers repeat across fiscal years, so BELNR alone is not a unique key.
Accounting documents from BKPF
An accounting document created from billing carries the billing document number in AWKEY, and the reference transaction VBRK in AWTYP.
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).AWKEY is a character field. Fill it exactly as stored, including leading zeros, or the query returns nothing.
Common errors
- Reading VBFA in only one direction. Querying
vbelvfinds followers. To find predecessors, query onvbelninstead. - Ignoring items. Without
POSNVandPOSNNin the condition, a partial delivery looks like a link for the whole order. - Wrong
AWKEYformat. Some reference types buildAWKEYfrom more than the document number, for example number plus year. Check a known document in SE16N first. - Treating an empty result as an error. A missing row can be a real gap, such as an order not yet billed.
Chaining the queries
To trace an order to its accounting documents, select billing documents from lt_followers where vbtyp_n is the billing category, then use those numbers as AWKEY values. Use FOR ALL ENTRIES or a join, and check that the driving table is not empty first. An empty driving table makes FOR ALL ENTRIES select every row.
Exercise
You query VBFA with WHERE vbelv = a sales order number and get a delivery and a billing document. How do you find the accounting document for the billing document, and which two BKPF fields do you filter on?
Show answer
Select from BKPF where AWTYP = 'VBRK' and AWKEY equals the billing document number from VBFA (VBELN of the row with the billing category). Check the AWKEY formatting against a known document first.
Exercise
Why must you check that the driving internal table is not empty before using FOR ALL ENTRIES?
Show answer
If the driving table is empty, FOR ALL ENTRIES drops the condition and the SELECT returns all rows of the database table, which gives wrong results and a heavy load.