SAP Sales Order ABAP SELECT Code Generator
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.
Fill in
Order header and items
REPORT ZSD_ORDER_LIST.
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 500 ROWS
WHERE vbak~vkorg = '1000'
AND vbak~auart = 'OR'
AND vbak~erdat BETWEEN '20260101' AND '20261231'
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
* 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.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.
Skip the ABAP for reporting
Optional: a short call about answering SD reporting questions in plain language, read-only.
Optional. The tool and every export work without this. No spam.
How to use the generator
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
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
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.
- Filter on indexed fields
- Limit rows during testing
- Use ALPHA conversion for document numbers
- Re-check status logic after migration to S/4HANA
Frequently Asked Questions
How do I join VBAK and VBAP in ABAP?
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?
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?
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?
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.
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.
Related Tools
SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
Fill in order type, sales area, customer, material and quantity to get an ABAP skeleton that creates a sales order through the standard BAPI.
SAPSAP SD Tables Reference (VBAK, VBAP, LIKP, VBRK and More)
Lookup of the main SAP SD tables with their key fields and what changed in S/4HANA. Search, then export to Excel or PDF.
SAPSAP SD Transaction Codes Reference
A searchable list of common SAP sales and distribution transaction codes, grouped by process area, for daily use and onboarding.
SAPSAP Order-to-Cash (SD) Health Assessment
Eight questions that score how well your SAP SD order to cash design and operation hold up, with recommendations by band.
More Developer Tools tools
See all- SAP ABAP Open Items SELECT Generator
- ABAP BAPI Return Handling and Commit Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
- ABAP Unit Test Class Generator
- SAP ABAP AUTHORITY-CHECK Snippet Generator