Sub topic 1
Documents in SAP: headers, items and numbers
Every business transaction in SAP leaves a document behind. Document flow is built from the links between those documents. Before you can follow a link, you need to know what one document looks like and how SAP identifies it. This chapter covers the header and item structure, document numbers, and the number formats that cause most lookup errors.
Prerequisites
This chapter assumes you can do the following:
- Navigate the SAP GUI and call transactions such as VA03, ME23N and FB03.
- Name the main modules involved: SD (Sales and Distribution), MM (Materials Management) and FI (Financial Accounting).
- Open a table in SE16 or SE16N and read its fields.
- Read a basic ABAP
SELECTstatement. Only the code example near the end needs this.
What a business document is
A business document is a database record set that stores one business event. A sales order records that a customer asked for goods. A delivery records that goods are to be moved. An invoice records that money is owed. Each document has its own number, its own data and its own life cycle.
Documents are not free text. Each belongs to a category such as order, delivery or invoice, and the system decides which actions are possible from that category. Later chapters cover categories in more detail. For now, treat each document as one uniquely numbered record that can point to other records.
Header and item structure
Almost every SAP document has two levels.
- The header holds data that applies to the whole document. Examples are the customer or vendor, the document date, the document type, the currency and the creating user.
- The items hold one line per product, service or posting. Examples are material, quantity, unit of measure, price and plant.
A sales order with three products has one header record and three item records. The items depend on the header. If the header is deleted, the items go with it.
Header tables hold one row per document. Item tables hold one row per item and repeat the header key. This repeated key is what makes the header and item tables joinable.
Header and item tables by module
Each document type has its own pair of tables. The names are fixed, so learn the common ones.
| Document | Header table | Item table | Number field |
|---|---|---|---|
| Sales order, quotation, return | VBAK | VBAP | VBELN |
| Outbound delivery | LIKP | LIPS | VBELN |
| Billing document | VBRK | VBRP | VBELN |
| Purchase order | EKKO | EKPO | EBELN |
| Material document | MKPF | MSEG | MBLNR |
| Accounting document | BKPF | BSEG | BELNR |
Three of these tables use the field name VBELN for different documents. A value of 0000004711 in VBELN means nothing until you know which table it came from. An order, a delivery and an invoice can all carry the same number, because each is numbered in its own sequence. Document flow tables exist because the number alone does not say which kind of document you hold.
Document numbers
A document number is the primary key of the header. SAP assigns it from a number range, which is a defined interval of numbers managed by a number range object. You maintain number ranges in transaction SNRO, and many are assigned through customizing, for example to the document type.
A number range has these properties:
- Internal assignment. The system gives the next free number when the document is saved. This is the usual setting for sales orders and invoices.
- External assignment. The user or an interface supplies the number. The number must fall inside the allowed interval.
- Buffering. The system fetches numbers in blocks to save database accesses. A block that is not used is lost when the application server restarts or a transaction is cancelled.
Because of buffering, a gap in document numbers is normal and does not by itself indicate a problem. Do not use gaps to detect missing documents.
Keys beyond the document number
The number alone does not always identify a document. Check the whole key.
- Accounting documents are identified by company code (
BUKRS), document number (BELNR) and fiscal year (GJAHR). The sameBELNRcan occur in several company codes and years. - Material documents are identified by
MBLNRand yearMJAHR. - Purchase orders and sales documents are identified by the number alone, within a client.
Every table also begins with the client field MANDT. Open SQL in ABAP applies the client filter for you, but SE16 and native tools may not.
Item numbers
An item is identified by the header key plus an item number. The item number field has a different name and length in each module.
| Area | Item field | Length | Typical step |
|---|---|---|---|
| Sales and delivery | POSNR | 6 | 10, 20, 30 |
| Purchasing | EBELP | 5 | 10, 20, 30 |
| Accounting | BUZEI | 3 | 1, 2, 3 |
| Material document | ZEILE | 4 | 1, 2, 3 |
Item numbers in sales and purchasing usually increase in steps of ten. This leaves space to insert items between existing ones without renumbering. Accounting line items are numbered consecutively.
The different lengths matter when you trace links. A sales item is 000010, and the matching delivery item may be 000010 or a different number, such as 900001. Never assume that an item keeps its number across documents. The link between items is stored explicitly, as later chapters show.
Leading zeros and conversion exits
Numeric document numbers are stored as character fields padded with leading zeros. Order 4711 is stored as 0000004711. SAP screens hide the zeros through a conversion exit, which is a routine attached to a data element that converts between the display format and the stored format. Most document number fields use the exit ALPHA.
The GUI converts values for you. SE16 does so on its selection screen, and ABAP selection screen parameters do so when typed with the right data element. Your own code does not convert values automatically, and a comparison against '4711' finds nothing.
REPORT z_doc_basics.
DATA lv_input TYPE c LENGTH 10 VALUE '4711'.
START-OF-SELECTION.
DATA(lv_order) = CONV vbeln_va( |{ lv_input ALPHA = IN }| ).
SELECT SINGLE vbeln, erdat, auart, kunnr
FROM vbak
WHERE vbeln = @lv_order
INTO @DATA(ls_head).
IF sy-subrc <> 0.
WRITE: / 'Order', lv_order, 'not found'.
RETURN.
ENDIF.
SELECT posnr, matnr, kwmeng, vrkme
FROM vbap
WHERE vbeln = @lv_order
ORDER BY posnr
INTO TABLE @DATA(lt_items).
WRITE: / 'Order', ls_head-vbeln, 'type', ls_head-auart,
'customer', ls_head-kunnr.
LOOP AT lt_items INTO DATA(ls_item).
WRITE: / ls_item-posnr, ls_item-matnr, ls_item-kwmeng, ls_item-vrkme.
ENDLOOP.The string template with ALPHA = IN pads the input with zeros to the field length. The report then reads one header row and its item rows with the same key. Use ALPHA = OUT to remove the zeros for display.
Common errors
- Searching without zeros. A
WHERE vbeln = '4711'clause returns nothing. Convert the input first. - Treating the number as unique across tables. The same
VBELNvalue can exist as an order and as an invoice. Always pair the number with the table or the document category. - Forgetting the fiscal year. A
BELNRlookup in BKPF withoutBUKRSandGJAHRcan return several unrelated documents. - Joining items with the wrong item length. Moving a six-digit
POSNRinto a three-digitBUZEItruncates it silently. - Reading the header and missing items. A header can exist with its items rejected, deleted or still in a draft state. Check both levels.
Exercise
A colleague runs SELECT * FROM bkpf WHERE belnr = '100000123' in a program and gets rows from two company codes. Name two things wrong or missing in the query, and say why the result is ambiguous.
Show answer
The query uses a nine-character value for a ten-character field. The ALPHA conversion was not applied, so the value is 100000123, not 0100000123. The query also omits company code (BUKRS) and fiscal year (GJAHR). The accounting document key is BUKRS, BELNR and GJAHR, so the same BELNR can exist in several company codes and years. Without those fields, the number does not identify one document.
Exercise
Sales order 4711 has item 000010. The delivery created from it also has the number 4711 in the field VBELN. Is it the same document? How do you tell the two apart?
Show answer
No. Orders are stored in VBAK and deliveries in LIKP, and each table has its own number range, so equal numbers do not mean the same document. You tell them apart by the table or the document category. The link between the two documents is stored in the document flow, which later chapters cover.