# SAP Sales Order ABAP SELECT Code Generator

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

## Short answer

Fill in sales organization, order type and date range to get a ready ABAP report skeleton that joins VBAK and VBAP and reads VBFA.

## Category

developer

## Erp

SAP ECC / S/4HANA

## Audience

- ABAP developer
- SAP SD consultant

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Report name

**Key:** report_name

**Type:** text

**Default:** ZSD_ORDER_LIST

#### Sales organization

**Key:** vkorg

**Type:** text

**Default:** 1000

#### Sales document type

**Key:** auart

**Type:** text

**Default:** OR

#### Created from (YYYYMMDD)

**Key:** date_from

**Type:** text

**Default:** 20260101

#### Created to (YYYYMMDD)

**Key:** date_to

**Type:** text

**Default:** 20261231

#### Maximum rows

**Key:** max_rows

**Type:** number

**Default:** 500

**Blocks:**

#### Order header and items

**Body:** REPORT {{report_name}}.

TYPES: BEGIN OF ty_order,
         vbeln  TYPE vbak-vbeln,
         erdat  TYPE vbak-erdat,
         auart  TYPE vbak-auart,
         kunnr  TYPE vbak-kunnr,
         waerk  TYPE vbak-waerk,
         posnr  TYPE vbap-posnr,
         matnr  TYPE vbap-matnr,
         kwmeng TYPE vbap-kwmeng,
         vrkme  TYPE vbap-vrkme,
         netwr  TYPE vbap-netwr,
       END OF ty_order.

DATA: lt_order TYPE STANDARD TABLE OF ty_order,
      ls_order TYPE ty_order.

SELECT vbak~vbeln vbak~erdat vbak~auart vbak~kunnr vbak~waerk
       vbap~posnr vbap~matnr vbap~kwmeng vbap~vrkme vbap~netwr
  FROM vbak INNER JOIN vbap ON vbap~vbeln = vbak~vbeln
  INTO TABLE lt_order UP TO {{max_rows}} ROWS
  WHERE vbak~vkorg = '{{vkorg}}'
    AND vbak~auart = '{{auart}}'
    AND vbak~erdat BETWEEN '{{date_from}}' AND '{{date_to}}'
  ORDER BY vbak~vbeln vbap~posnr.

IF sy-subrc <> 0.
  WRITE: / 'No sales orders found for the selection'.
  RETURN.
ENDIF.

LOOP AT lt_order INTO ls_order.
  WRITE: / ls_order-vbeln, ls_order-posnr, ls_order-matnr, ls_order-kwmeng, ls_order-vrkme, ls_order-netwr, ls_order-waerk.
ENDLOOP.

#### Subsequent documents from VBFA

**Body:** * Document flow: deliveries (J) and invoices (M) created from the orders above
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,
       END OF ty_flow.

DATA lt_flow TYPE STANDARD TABLE OF ty_flow.

IF lt_order IS NOT INITIAL.
  SELECT vbelv posnv vbeln posnn vbtyp_n
    FROM vbfa
    INTO TABLE lt_flow
    FOR ALL ENTRIES IN lt_order
    WHERE vbelv = lt_order-vbeln
      AND posnv = lt_order-posnr
      AND ( vbtyp_n = 'J' OR vbtyp_n = 'M' ).
ENDIF.

**Notes:** Verify field names in SE11 on your release. Dates are stored as YYYYMMDD. FOR ALL ENTRIES returns no duplicates, so sort and delete duplicates as needed. On S/4HANA, prefer CDS views or released APIs for new development and check the custom code with ABAP Test Cockpit. Never run unreviewed code in production.

## Content

**Intro:** Most SD reports start with the same join: order header, order item, then the document flow. This generator produces a clean ABAP skeleton for that pattern with your sales organization, order type and date range filled in.

**Sections:**

#### How to use the generator

**Body:** Fill the six fields on the left, then copy the code into SE38 or ADT and activate it. The first block reads headers and items with an inner join and a row limit, the second finds delivery and invoice documents for those items through VBFA. Adjust the selected fields to what your report needs rather than selecting everything.

#### Why the filters are on VBAK

**Body:** VBAK holds sales organization, order type and creation date, so filtering there before the join keeps the result small. The row limit protects the system from an accidental full table read. If you need a customer filter, add a select option on VBAK-KUNNR. Use the document category in VBFA-VBTYP_N, where J is delivery and M is invoice, to avoid unrelated links.

#### Mistakes to avoid

**Body:** Do not use SELECT with no WHERE clause on VBAP, which can be among the largest tables in the system. Do not forget the leading zeros on document numbers when you compare to a text field, use the ALPHA conversion. Remember that statuses sit in VBUK on older releases and in the document tables on S/4HANA, so do not copy status joins between releases.

**Bullets:**

- Filter on indexed fields
- Limit rows during testing
- Use ALPHA conversion for document numbers
- Re-check status logic after migration to S/4HANA

**Cta:** If you would rather ask for this data in plain language than write selects, Netray can show you a read-only approach over your SAP data. Ask for a review.

**Faq:**

#### How do I join VBAK and VBAP in ABAP?

**Answer:** Use an inner join on the sales document number, with the condition vbap~vbeln = vbak~vbeln. Put the header filters such as sales organization and creation date in the WHERE clause so the database filters before returning rows.

#### How do I find the delivery for a sales order item in ABAP?

**Answer:** Select from VBFA where VBELV is the order number and POSNV is the item, and filter VBTYP_N with J for delivery. The returned VBELN and POSNN are the delivery and delivery item, which you can read from LIKP and LIPS.

#### Does this code work on S/4HANA?

**Answer:** The VBAK, VBAP and VBFA selects work in S/4HANA, but status logic and pricing tables changed. Check your release and run the custom code check. For new development SAP recommends released CDS views and APIs.

#### Why is my select slow?

**Answer:** Usually the WHERE clause does not use an index, or FOR ALL ENTRIES is run on a very large driver table. Filter on indexed fields such as VKORG with date, limit rows, and use ST05 to see the actual database access.

## Search terms

abap select vbak vbap, abap sales order report, sap sd abap code, vbak vbap join abap, abap vbfa document flow, sap open sql sales order, abap report sales orders, sap sd select statement

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