Skip to main content

SAP ABAP REAL-TIME ALV Report: Customer Sales with Subtotals Using REUSE_ALV_GRID_DISPLAY

SAP ABAP DEVELOPMENT ALV REPORTS : 

✅ Real-Time SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY (KNA1 + VBAK + VBAP)

Here is a real-time SAP ABAP ALV program using REUSE_ALV_GRID_DISPLAY, which joins the KNA1 (Customer Master), VBAK (Sales Order Header), and VBAP (Sales Order Item) tables. It displays:

  1. Customer Number
  2. Customer Name
  3. Sales Document
  4. Material (Item)
  5. Net Price
With subtotals on Net Price grouped by Customer

SAP ABAP ALV Programming Tutorial | Steps, Code, and Real-Time ALV Report Development

REPORT ZMM_ALV_KNA1_VBAP.
TABLESkna1vbakvbap.
TYPESBEGIN OF ty_data,
         kunnr TYPE kna1-kunnr,
         name1 TYPE kna1-name1,
         vbeln TYPE vbak-vbeln,
         matnr TYPE vbap-matnr,
         netwr TYPE vbap-netwr,
       END OF ty_data.
DATAit_data   TYPE TABLE OF ty_data,
      wa_data   TYPE ty_data,
      it_fcat   TYPE slis_t_fieldcat_alv,
      wa_fcat   TYPE slis_fieldcat_alv,
      wa_layout TYPE slis_layout_alv.

* Step 1: Fetch Data using joins
SELECT a~kunnr b~name1 a~vbeln c~matnr c~netwr
  INTO TABLE it_data
  FROM vbak AS a
  INNER JOIN kna1 AS ON a~kunnr b~kunnr
  INNER JOIN vbap AS ON a~vbeln c~vbeln
  UP TO 100 ROWS.
* Step 2: Field Catalog for ALV
CLEAR wa_fcat.
wa_fcat-fieldname 'KUNNR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Customer No'.
wa_fcat-do_sum    ' '.
wa_fcat-key       'X'.
APPEND wa_fcat TO it_fcat.
CLEAR wa_fcat.

wa_fcat-fieldname 'NAME1'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Customer Name'.
wa_fcat-do_sum    ' '.
wa_fcat-key       'X'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'VBELN'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Sales Document'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'MATNR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Material'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'NETWR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Net Price'.
wa_fcat-do_sum    'X'.  " This enables subtotal/total
APPEND wa_fcat TO it_fcat.
* Step 3: Layout for subtotal display
wa_layout-colwidth_optimize 'X'.
wa_layout-zebra             'X'.
wa_layout-totals_text       'Total'.

* Step 4: ALV Grid Display
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program sy-repid
    is_layout          wa_layout
    it_fieldcat        it_fcat
    i_save             'A'
  TABLES
    t_outtab           it_data
  EXCEPTIONS
    program_error      1
    OTHERS             2.

IF sy-subrc <> 0.
  MESSAGE 'Error displaying ALV output' TYPE 'E'.
ENDIF.
THE OUTPUT IMAGE:


SAP ABAP ALV REPORT OUT PUT SCREEN

REAL-TIME SCENARIO  PROGRAM 2

Example 2: Real-Time SAP ABAP ALV Report Using EKKO and EKPO with Subtotals

📘 Real-Time Business Scenario (Procurement)

In a procurement department, buyers often need to monitor purchase orders (POs) to:

  1. Track which vendors are supplying which materials
  2. Monitor PO item prices
  3. Analyze PO value by vendor or material

💼 A typical requirement from the client/business:

“Show me a list of POs with vendor name, material number, and item net value. Also show subtotals by Vendor so we know how much we’re ordering from each.”

This is a real-time report used in MM (Materials Management) module to:

  1. Audit procurement spend
  2. Compare vendor performance
  3. Export PO reports to Excel



🧾 Required Fields

FieldTableDescription
EBELNEKKOPurchasing Document No
LIFNREKKOVendor Number
NAME1LFA1Vendor Name
MATNREKPOMaterial Number
NETWREKPONet Price

✅ Full Program Code Using REUSE_ALV_GRID_DISPLAY


REPORT ZMM_ALV_EKKO_EKPO.
TABLESekkoekpolfa1.

TYPESBEGIN OF ty_po_data,
         ebeln TYPE ekko-ebeln,
         lifnr TYPE ekko-lifnr,
         name1 TYPE lfa1-name1,
         matnr TYPE ekpo-matnr,
         netwr TYPE ekpo-netwr,
       END OF ty_po_data.

DATAit_data   TYPE TABLE OF ty_po_data,
      wa_data   TYPE ty_po_data,
      it_fcat   TYPE slis_t_fieldcat_alv,
      wa_fcat   TYPE slis_fieldcat_alv,
      wa_layout TYPE slis_layout_alv.

*----------------------------------------------------------------------
* Step 1: Fetch PO data with Vendor and Item details
*----------------------------------------------------------------------
SELECT a~ebeln a~lifnr b~name1 c~matnr c~netwr
  INTO TABLE it_data
  FROM ekko AS a
  INNER JOIN lfa1 AS ON a~lifnr b~lifnr
  INNER JOIN ekpo AS ON a~ebeln c~ebeln
  UP TO 100 ROWS.

IF it_data IS INITIAL.
  MESSAGE 'No purchase order data found' TYPE 'I'.
  EXIT.
ENDIF.

*----------------------------------------------------------------------
* Step 2: Create Field Catalog
*----------------------------------------------------------------------

CLEAR wa_fcat.
wa_fcat-fieldname 'EBELN'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'PO Number'.
wa_fcat-key       'X'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'LIFNR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Vendor Number'.
wa_fcat-key       'X'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'NAME1'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Vendor Name'.
wa_fcat-key       'X'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'MATNR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Material'.
APPEND wa_fcat TO it_fcat.

CLEAR wa_fcat.
wa_fcat-fieldname 'NETWR'.
wa_fcat-tabname   'IT_DATA'.
wa_fcat-seltext_m 'Net Price'.
wa_fcat-do_sum    'X'.  " Enables subtotal
APPEND wa_fcat TO it_fcat.

*----------------------------------------------------------------------
* Step 3: Layout Settings
*----------------------------------------------------------------------

wa_layout-colwidth_optimize 'X'.
wa_layout-zebra             'X'.
wa_layout-totals_text       'Total Value'.

*----------------------------------------------------------------------
* Step 4: Display ALV Report
*----------------------------------------------------------------------

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program sy-repid
    is_layout          wa_layout
    it_fieldcat        it_fcat
    i_save             'A'
  TABLES
    t_outtab           it_data
  EXCEPTIONS
    program_error      1
    OTHERS             2.

IF sy-subrc <> 0.
  MESSAGE 'ALV Display Failed' TYPE 'E'.
ENDIF.

ABAP ALV REPORT OUT PUT SCREEN



SAP ABAP ALV REPORTS



































Comments

Popular posts from this blog

SAP ABAP SE11 Data Dictionary Tutorial | Step-by-Step Explanation with Real-Time Examples

    SAP ABAP Data Dictionary (SE11) – Complete Guide to Tables, Views, Domains & Data Elements    What is the ABAP Data Dictionary? The SAP ABAP data dictionary serves as a primary and structured source of data for creating objects. It is an independent database DDL (Data Definition Language) that primarily deals with creating, editing, and dropping database tables. The data dictionary allows you to define and maintain database-related items. The ABAP data dictionary is readily connected with the ABAP workbench, allowing all of the workbench's components to simply access the definitions contained in the dictionary. Functions of Data Dictionary The important functions of data dictionary objects are as follows. 1. Database tables 2. Domains 3. Data elements 4. Views 5. Search helps 6. Lock Objects 1. Database Tables: –  Database tables are collections of fields that store physical data. It is an object that holds data in the form of rows and columns. So databa...

SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Tutorial with Examples

  📘 SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Step-by-Step Guide (With Real Examples) SAP ABAP developers frequently use ALV (ABAP List Viewer) to present data in a clean and interactive format. One of the most commonly used ALV function modules in classical reporting is: This function module allows developers to create professional ALV reports with features such as sorting, filtering, totals, layout saving, and exporting to Excel. In this guide, I will explain: What REUSE_ALV_GRID_DISPLAY is How field catalogs work How to build an ALV report using MARC and KNA1 tables Real-time business examples Clean, fully working ABAP code This post is written from my personal learning and hands-on experience while working on SAP ABAP reports. 🧩 What is REUSE_ALV_GRID_DISPLAY? REUSE_ALV_GRID_DISPLAY is a function module-based ALV used in SAP ECC and compatible with S/4HANA systems. It displays internal table data in a grid-style output , which is more user...

SAP ABAP Control Break Statements – Complete Guide with AT FIRST, AT NEW & AT END OF Examples

📘 SAP ABAP Control Break Statements – Complete Guide with Example Learn about Control Break Statements in SAP ABAP including AT FIRST, AT NEW, AT END OF, AT LAST, and ON CHANGE OF. Includes a real-time example program with explanation and output. Perfect for SAP ABAP beginners and developers. SAP ABAP Control Break Statements AT NEW ABAP example AT END OF ABAP tutorial ABAP loop control SAP ABAP report programming AT FIRST, AT LAST in ABAP SAP ABAP internal table sorted 📘 Introduction In SAP ABAP , control break statements are used to trigger events inside a loop . They are essential for creating grouped or hierarchical reports , especially when working with sorted internal tables. There are 5 main control break statements in ABAP: AT FIRST – ENDAT AT NEW – ENDAT AT END OF – ENDAT AT LAST – ENDAT ON CHANGE OF FIELD. SAP ABAP Loop Control Tutorial:  How to Use AT NEW, AT END OF, AT FIRST, AT LAST, & ON CHANGE OF  FIELD. 🧠 Key Control Break Statements Explai...