ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Sales document flow: follow a sales order to its deliveries and invoices with VBFA

The problem

A customer asks: what happened with order 12345?

How we solve it

  1. 1The orderstarting point
  2. 2Read VBFAwhat follows
  3. 3Showreadable types

The result

Each follow-up document of the order is printed with its type.

SAP objects in play

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A customer asks: what happened with order 12345? The program must show which deliveries and invoices were created from that sales order, so that support can answer in seconds and does not have to click through VA03, VL03N and VF03.

2. How we design it

Whenever SAP creates a follow-up document (delivery, invoice, goods movement), it records the link in the table VBFA, the document flow. Each row says: from this preceding document (VBELV) came this subsequent document (VBELN), and the field VBTYP_N tells what kind of document it is. We read the rows where the preceding document is our order, translate the type code into a word and print them. The same query, repeated with the delivery as preceding document, would follow the chain one step further.

Why we decided it this way

  1. 1VBFA instead of searching every document tableOne table holds the links of the whole sales process. Searching VBAK, LIKP and VBRK separately would take three queries and still not show the links.
  2. 2A SWITCH for the document typeVBTYP_N is a one-letter code. The portal's readers want words, and a SWITCH keeps the translation in one place.
  3. 3ORDER BY the follow-up documentA stable order makes the output comparable between runs and between systems.

Before you start

  • A sales order that has at least one delivery or invoice. Look at its flow in VA03: menu Environment > Display Document Flow.
  • ADT and a package such as $TMP.

3. The code, step by step

Step 1 of 3: Name the order

A runnable class with the order number you want to follow.

ABAP · Class · ZCL_DOC_FLOW (frame)
" Class frame and order number
CLASS zcl_doc_flow DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_doc_flow IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(order_no) = CONV vbeln_va( '0000012345' ).
Part by part (plain text)
  1. Class frame and order number. VBELN_VA is the data element of a sales document number, ten digits with leading zeros. Replace the number with an order of your system.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

SAP tables

4. The final outcome

Each follow-up document of the order is printed with its type.

What you should see

Example output (your system has other data): Flow of sales order 12345 Delivery 80000123 Invoice 90000067 Goods movement 49000045

Try it and check the result

  • Run with F9 for an order that has been delivered and invoiced. The console lists the deliveries, goods movements and invoices.
  • Compare with VA03: Environment > Display Document Flow shows the same documents.
  • Run it for an order that is still open: the program says that there are no follow-up documents.

Common mistakes

  • VBFA links item to item. If you need the link on item level, keep POSNV and POSNN instead of removing the duplicates.
  • The chain has more steps than the first one. To go from the delivery to the invoice, repeat the SELECT with the delivery as VBELV.
  • Some categories are different in other releases or industry solutions. Always keep an ELSE branch for the unknown ones.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP S/4HANA or ECC system (for example an S/4HANA trial appliance from SAP Cloud Appliance Library). The ABAP trial images do not contain these tables. Not released for ABAP Cloud.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.