# SAP SD Document Flow Table (VBFA) ABAP Generator

> Source: https://netray.co/tools/sap-ecc-sd-document-flow-vbfa-abap-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Enter a sales order and the follow-on document categories you want, and get an ABAP report that reads VBFA from order to delivery to invoice and goods movement, with VBUK and VBUP status.

## Category

developer

## Erp

SAP ECC 6.0

## Audience

- ABAP developer
- SAP SD consultant
- SAP support analyst

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Start document number (10 digits)

**Key:** vbeln

**Type:** text

**Default:** 0000012345

#### Start document category (VBTYP_V)

**Key:** vbtyp_v

**Type:** select

**Options:**

- C
- H
- K
- L
- J

**Default:** C

**Help:** C order, H returns, K credit memo request, L debit memo request, J delivery.

#### Follow-on categories, letters without spaces

**Key:** cats

**Type:** text

**Default:** JMRNO

**Help:** J delivery, M invoice, R goods movement, N invoice cancellation, O credit memo, T returns delivery.

**Blocks:**

#### Declarations and selection screen

**Body:** REPORT zsd_document_flow.

* Document flow for {{vbeln}} (category {{vbtyp_v}}), follow-ons {{cats}}
* VBTYP: C order, J delivery, M invoice, N invoice cancellation,
*        O credit memo, R goods movement, H returns, T returns delivery

TYPES: BEGIN OF ty_flow,
         vbelv   TYPE vbfa-vbelv,
         posnv   TYPE vbfa-posnv,
         vbeln   TYPE vbfa-vbeln,
         posnn   TYPE vbfa-posnn,
         vbtyp_n TYPE vbfa-vbtyp_n,
         vbtyp_v TYPE vbfa-vbtyp_v,
         rfmng   TYPE vbfa-rfmng,
         meins   TYPE vbfa-meins,
         erdat   TYPE vbfa-erdat,
       END OF ty_flow.

DATA: gt_flow  TYPE STANDARD TABLE OF ty_flow,
      gt_deliv TYPE STANDARD TABLE OF ty_flow,
      gs_flow  TYPE ty_flow,
      gr_vbtyp TYPE RANGE OF vbfa-vbtyp_n,
      gs_vbtyp LIKE LINE OF gr_vbtyp,
      gv_cats  TYPE c LENGTH 20,
      gv_len   TYPE i,
      gv_off   TYPE i,
      gs_vbuk  TYPE vbuk,
      gt_vbup  TYPE STANDARD TABLE OF vbup,
      gs_vbup  TYPE vbup.

PARAMETERS: p_vbeln TYPE vbfa-vbelv   DEFAULT '{{vbeln}}' OBLIGATORY,
            p_vbtyp TYPE vbfa-vbtyp_v DEFAULT '{{vbtyp_v}}' OBLIGATORY.

#### Level 1 and level 2 flow from VBFA

**Body:** START-OF-SELECTION.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = p_vbeln
    IMPORTING
      output = p_vbeln.

* Build a range of follow-on categories from the letters
  gv_cats = '{{cats}}'.
  gv_len = strlen( gv_cats ).
  DO gv_len TIMES.
    gv_off = sy-index - 1.
    CLEAR gs_vbtyp.
    gs_vbtyp-sign   = 'I'.
    gs_vbtyp-option = 'EQ'.
    gs_vbtyp-low    = gv_cats+gv_off(1).
    APPEND gs_vbtyp TO gr_vbtyp.
  ENDDO.

* Level 1: direct successors of the start document (uses the VBELV key)
  SELECT vbelv posnv vbeln posnn vbtyp_n vbtyp_v rfmng meins erdat
    FROM vbfa
    INTO TABLE gt_flow
    WHERE vbelv   = p_vbeln
      AND vbtyp_v = p_vbtyp
      AND vbtyp_n IN gr_vbtyp.

* Level 2: successors of each delivery (invoices, goods movements)
  SELECT vbelv posnv vbeln posnn vbtyp_n vbtyp_v rfmng meins erdat
    FROM vbfa
    INTO TABLE gt_deliv
    WHERE vbelv   = p_vbeln
      AND vbtyp_v = p_vbtyp
      AND vbtyp_n = 'J'.
  SORT gt_deliv BY vbeln.
  DELETE ADJACENT DUPLICATES FROM gt_deliv COMPARING vbeln.

  IF gt_deliv IS NOT INITIAL.
    SELECT vbelv posnv vbeln posnn vbtyp_n vbtyp_v rfmng meins erdat
      FROM vbfa
      APPENDING TABLE gt_flow
      FOR ALL ENTRIES IN gt_deliv
      WHERE vbelv   = gt_deliv-vbeln
        AND vbtyp_v = 'J'
        AND vbtyp_n IN gr_vbtyp.
  ENDIF.

  SORT gt_flow BY vbelv posnv vbeln posnn vbtyp_n.
  DELETE ADJACENT DUPLICATES FROM gt_flow
    COMPARING vbelv posnv vbeln posnn vbtyp_n.

#### Header status (VBUK), item status (VBUP) and output

**Body:**   SELECT SINGLE * FROM vbuk INTO gs_vbuk WHERE vbeln = p_vbeln.
  SELECT * FROM vbup INTO TABLE gt_vbup WHERE vbeln = p_vbeln.

* Status values: space not relevant, A not processed,
*                B partially processed, C completely processed
  WRITE: / 'GBSTK', gs_vbuk-gbstk, 'LFSTK', gs_vbuk-lfstk,
           'FKSTK', gs_vbuk-fkstk, 'WBSTK', gs_vbuk-wbstk,
           'CMGST', gs_vbuk-cmgst.
  LOOP AT gt_vbup INTO gs_vbup.
    WRITE: / gs_vbup-posnr, 'LFSTA', gs_vbup-lfsta,
             'FKSTA', gs_vbup-fksta, 'GBSTA', gs_vbup-gbsta,
             'ABSTA', gs_vbup-absta.
  ENDLOOP.
  ULINE.
  LOOP AT gt_flow INTO gs_flow.
    WRITE: / gs_flow-vbelv, gs_flow-posnv, gs_flow-vbtyp_v, '->',
             gs_flow-vbeln, gs_flow-posnn, gs_flow-vbtyp_n,
             gs_flow-rfmng UNIT gs_flow-meins, gs_flow-meins,
             gs_flow-erdat.
  ENDLOOP.

#### Status field decode

**Table:**

**Columns:**

- Table-field
- Meaning
- A / B / C

**Rows:**

**0:** VBUK-GBSTK

**1:** Overall processing status of the document

**2:** Open / in process / completed

**0:** VBUK-LFSTK

**1:** Delivery status (header)

**2:** Not delivered / partly / fully

**0:** VBUK-FKSTK

**1:** Billing status (header)

**2:** Not billed / partly / fully

**0:** VBUK-WBSTK

**1:** Total goods movement status

**2:** Not posted / partly / fully

**0:** VBUK-CMGST

**1:** Overall credit check status

**2:** A checked, OK / B checked, not OK (blocked) / C not OK, partly released; D released by credit representative

**0:** VBUP-LFSTA

**1:** Delivery status (item)

**2:** Not delivered / partly / fully

**0:** VBUP-FKSTA

**1:** Billing status (item)

**2:** Not billed / partly / fully

**0:** VBUP-GBSTA

**1:** Overall processing status (item)

**2:** Open / in process / completed

**0:** VBUP-ABSTA

**1:** Rejection status (item)

**2:** Not rejected / partly / fully rejected

#### Alternative: standard function RV_ORDER_FLOW_INFORMATION

**Body:** * Same information as the VA03 document flow screen, all levels.
  DATA: gs_comwa TYPE vbco6,
        gt_vbfa  TYPE STANDARD TABLE OF vbfa.

  gs_comwa-vbeln = p_vbeln.
  CALL FUNCTION 'RV_ORDER_FLOW_INFORMATION'
    EXPORTING
      comwa         = gs_comwa
      nachfolger    = 'X'
      n_stufen      = '50'
      vorgaenger    = ' '
    TABLES
      vbfa_tab      = gt_vbfa
    EXCEPTIONS
      no_vbfa       = 1
      no_vbuk_found = 2
      OTHERS        = 3.

**Notes:** Verify in SE11 that VBFA has VBELV, POSNV, VBELN, POSNN, VBTYP_N, VBTYP_V, RFMNG, MEINS and ERDAT on your release, and in SE37 that RV_ORDER_FLOW_INFORMATION has COMWA (VBCO6), NACHFOLGER, N_STUFEN, VORGAENGER and table VBFA_TAB. In S/4HANA VBUK and VBUP no longer hold the data: the status fields moved into VBAK/VBAP, LIKP/LIPS and VBRK/VBRP, so this status block will not port as is.

## Content

**Intro:** This generator writes an ABAP report for SAP ECC 6.0 that traces the SD document flow of one sales document through VBFA, from order to delivery to invoice, goods movement, cancellation and credit memo. It adds the header and item processing status from VBUK and VBUP, which is where ECC keeps them.

**Sections:**

#### Which tables make up SD document flow in ECC

**Body:** VBFA is the document flow table: each row links a preceding document and item (VBELV, POSNV, category VBTYP_V) to a subsequent document and item (VBELN, POSNN, category VBTYP_N), with the referenced quantity RFMNG in unit MEINS and the creation date. Around it sit the document tables: VBAK and VBAP for sales documents, LIKP and LIPS for deliveries, VBRK and VBRP for billing, and MKPF and MSEG for the goods movement that VBFA lists as category R. Status is not on those tables in ECC: header status is in VBUK and item status in VBUP, both keyed by document number. Partners are in VBPA and schedule lines in VBEP.

**Bullets:**

- VBAK/VBAP to LIPS via LIPS-VGBEL and LIPS-VGPOS
- LIPS to VBRP via VBRP-VGBEL and VBRP-VGPOS; VBRP-AUBEL holds the order
- VBFA category R rows point to the material document number
- VBUK/VBUP: A not processed, B partly, C fully, blank not relevant

#### How the generated report reads the flow

**Body:** VBFA's primary key starts with VBELV and POSNV, so reading forward from a known document is a fast key access. The report reads level 1 (everything the start document points to) and then level 2 from each delivery, because invoices and goods movements for delivery-related billing are recorded against the delivery. The follow-on categories you type become a range on VBTYP_N, so JMR gives deliveries, invoices and goods movements only. Duplicates from the two levels are removed. The header line prints GBSTK, LFSTK, FKSTK, WBSTK and CMGST; each item prints LFSTA, FKSTA, GBSTA and ABSTA. If you want every level and predecessors as VA03 shows them, the last block calls RV_ORDER_FLOW_INFORMATION instead.

#### Reading backward and performance

**Body:** Going backward, for example from an invoice to its order, means filtering VBFA on VBELN, which is not the leading key field. Without a secondary index that is a full table scan on one of the largest SD tables. Better options: read VBRP-AUBEL and VBRP-VGBEL for the order and delivery behind an invoice item, or LIPS-VGBEL for the order behind a delivery item. Some systems have a customer index on VBFA-VBELN; check in SE11 before relying on it. Also restrict VBTYP_N: without it, a busy order with many partial deliveries and invoices can return hundreds of rows per item.

#### Version differences: ECC versus S/4HANA

**Body:** This code targets ECC 6.0. In S/4HANA the status tables VBUK and VBUP are obsolete as data stores: the status fields were moved into the document header and item tables (for example VBAK and VBAP for sales documents, LIKP and LIPS for deliveries). VBFA itself remains but was simplified, so test any VBFA logic again after a conversion. Code that reads VBUK directly is one of the standard findings in the S/4HANA custom code check, so if a conversion is coming, wrap these reads in one method you can replace later.

**Cta:** If your order desk wants to ask where an order stands, delivered, billed or blocked, in plain English against ECC on your own servers, we can build that read-only AI layer over the same tables.

**Faq:**

#### What is the SD document flow table in SAP?

**Answer:** VBFA is the sales document flow table. Each row links a preceding document and item (VBELV, POSNV) to a subsequent document and item (VBELN, POSNN), with the categories VBTYP_V and VBTYP_N, the referenced quantity RFMNG and its unit. VA03 Environment, Display document flow reads the same data.

#### What do the VBTYP values in VBFA mean?

**Answer:** Common values: C sales order, H returns, K credit memo request, L debit memo request, J delivery, T returns delivery, M invoice, N invoice cancellation, O credit memo, P debit memo and R goods movement. VBTYP_V is the category of the preceding document and VBTYP_N of the subsequent one. Check domain VBTYP in SE11 for the full list.

#### Which tables are used in SAP SD?

**Answer:** The core set is VBAK and VBAP (sales header and item), VBEP (schedule lines), VBPA (partners), LIKP and LIPS (deliveries), VBRK and VBRP (billing), VBFA (document flow) and, in ECC, VBUK and VBUP (header and item status). KNA1 and KNVV hold customer master data, and conditions are in KONV in ECC.

#### How do I find the invoice for a sales order without a slow VBFA query?

**Answer:** Read VBFA forward with VBELV = order number and VBTYP_N = M, which uses the key. If you only have the invoice and need the order, do not search VBFA on VBELN; read VBRP-AUBEL for the order and VBRP-VGBEL for the delivery instead, both on the invoice item.

## Search terms

sap sd document flow table, vbfa table in sap sd, sap table for sd document flow, tables used in sap sd, sap sd table relationship diagram, vbuk vbup status fields, rv_order_flow_information, vbtyp values sap

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
