Creating a Simple ALV Report Using REUSE_ALV_GRID_DISPLAY in SAP ABAP

To create an ALV report using REUSE_ALV_GRID_DISPLAY without using REUSE_ALV_FIELDCATALOG_MERGE, you can manually build the field catalog. This method gives you more control over the field properties and avoids issues with REUSE_ALV_FIELDCATALOG_MERGE.

 Steps to Create ALV Report with Manual Field Catalog

1. Define the Data Structure and Internal Table – The data structure and internal table store the material information.

2. Populate the Internal Table with Data – Use a SELECT statement to fetch the required data.

3. Create a Manual Field Catalog – Define the field catalog manually, specifying field properties such as field name, column headers, and alignment.

4. Display the ALV Grid Using REUSE_ALV_GRID_DISPLAY – Use the field catalog and internal table to display the ALV grid.

 Step 1: Define the Data Structure and Internal Table

The first step is to define the structure of the data you want to display and declare the internal table.

ABAP

TYPES: BEGIN OF ty_material,

         matnr TYPE matnr,  " Material Number

         maktx TYPE maktx,  " Material Description

         meins TYPE meins,  " Base Unit of Measure

         werks TYPE werks_d,  " plant

       END OF ty_material.

DATA: gt_materials TYPE TABLE OF ty_material.

ABAP

- ty_material defines the structure of material data.

- gt_materials is the internal table to hold the fetched data.

 Step 2: Populate the Internal Table

Use a SELECT statement to fill the internal table with data from the material master tables.

ABAP

SELECT mara~matnr makt~maktx mara~meins marc~labst

UP TO 100 ROWS

  INTO TABLE gt_materials

  FROM mara

  INNER JOIN makt ON mara~matnr = makt~matnr

  INNER JOIN marc ON mara~matnr = marc~matnr

  WHERE makt~spras = 'E'.

  

ABAP

This retrieves the material number, description, base unit of measure, and stock quantity.

  Step 3: Create a Manual Field Catalog

Instead of relying on REUSE_ALV_FIELDCATALOG_MERGE, we will manually build the field catalog using the structure slis_fieldcat_alv.

ABAP

DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,   " Field catalog table

      wa_fieldcat TYPE slis_fieldcat_alv. " Work area for field catalog.

" Clear the field catalog work area before use

CLEAR wa_fieldcat.

" Field for Material Number

wa_fieldcat-fieldname  = 'MATNR'.           " Field name from the internal table

wa_fieldcat-seltext_m  = 'Material Number'. " Column header

wa_fieldcat-col_pos    = 1.                 " Position of the column

wa_fieldcat-outputlen  = 18.                " Output length of the column

APPEND wa_fieldcat TO gt_fieldcat.           " Append field to the catalog

" Field for Material Description

CLEAR wa_fieldcat.

wa_fieldcat-fieldname  = 'MAKTX'.           " Field name from the internal table

wa_fieldcat-seltext_m  = 'Material Description'. " Column header

wa_fieldcat-col_pos    = 2.                 " Position of the column

wa_fieldcat-outputlen  = 40.            " Output length of the column

APPEND wa_fieldcat TO gt_fieldcat.

" Field for Base Unit of Measure

CLEAR wa_fieldcat.

wa_fieldcat-fieldname  = 'MEINS'.           " Field name from the internal table

wa_fieldcat-seltext_m  = 'Unit of Measure'. " Column header

wa_fieldcat-col_pos    = 3.                 " Position of the column

wa_fieldcat-outputlen  = 10.           " Output length of the column

APPEND wa_fieldcat TO gt_fieldcat.

 

" Field for Plant

CLEAR wa_fieldcat.

wa_fieldcat-fieldname  = 'WERKS'. " Field name from the internal table

wa_fieldcat-seltext_m  = 'Plant'.  " Column header

wa_fieldcat-col_pos    = 4.                 " Position of the column

wa_fieldcat-outputlen  = 10.             " Output length of the column

wa_fieldcat-do_sum     = 'X'.       " Allows summation for this field

APPEND wa_fieldcat TO gt_fieldcat.

ABAP

- fieldname: This is the name of the field from the internal table.

- seltext_m: This specifies the column header text.

- col_pos: The position of the field in the ALV output.

- outputlen: The display length of the column.

- do_sum: Allows summation for numeric fields like stock quantity.

  Step 4: Display Data Using REUSE_ALV_GRID_DISPLAY

Now, you can display the data using the ALV function module, passing the manually created field catalog and the internal table.

 

ABAP

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

  EXPORTING

    it_fieldcat = gt_fieldcat        " Field catalog table

  TABLES

    t_outtab    = gt_materials       " Internal table with data

  EXCEPTIONS

    program_error    = 1

    OTHERS           = 2.

IF sy-subrc <> 0.

  MESSAGE 'Error displaying ALV' TYPE 'E'.

ENDIF.

This function module displays the data in the ALV grid using the internal table gt_materials and the field catalog gt_fieldcat.

  Complete Code Example

ABAP

REPORT z_simple_alv_reuse.

TYPES: BEGIN OF ty_material,
         matnr TYPE matnr,  " Material Number
         maktx TYPE maktx,  " Material Description
         meins TYPE meins,  " Base Unit of Measure
         werks TYPE werks_d,  "Plant
       END OF ty_material.
DATA: gt_materials TYPE TABLE OF ty_material,
      gt_fieldcat  TYPE slis_t_fieldcat_alv,   " Field catalog table
      wa_fieldcat  TYPE slis_fieldcat_alv. "
Work area for field catalog.
SELECT mara~matnr makt~maktx mara~meins marc~werks
   UP TO 100 ROWS
  INTO TABLE gt_materials
  FROM mara
  INNER JOIN makt ON mara~matnr = makt~matnr
  INNER JOIN marc ON mara~matnr = marc~matnr
  WHERE makt~spras = 'E'.
" Clear the field catalog work area before use
CLEAR wa_fieldcat.

" Field for Material Number
wa_fieldcat-fieldname  = 'MATNR'.           " Field name from the internal table
wa_fieldcat-seltext_m  = 'Material Number'. " Column header
wa_fieldcat-col_pos    = 1.                 " Position of the column
wa_fieldcat-outputlen  = 18.                " Output length of the column
APPEND wa_fieldcat TO gt_fieldcat.         "
Append field to the catalog

" Field for Material Description
CLEAR wa_fieldcat.
wa_fieldcat-fieldname  = 'MAKTX'.           " Field name from the internal table
wa_fieldcat-seltext_m  = 'Material Description'. " Column header
wa_fieldcat-col_pos    = 2.                 " Position of the column
wa_fieldcat-outputlen  = 40.                " Output length of the column
APPEND wa_fieldcat TO gt_fieldcat.

" Field for Base Unit of Measure
CLEAR wa_fieldcat.
wa_fieldcat-fieldname  = 'MEINS'. " Field name from the internal table
wa_fieldcat-seltext_m  = 'Unit of Measure'. " Column header
wa_fieldcat-col_pos    = 3.                 " Position of the column
wa_fieldcat-outputlen  = 10.                " Output length of the column
APPEND wa_fieldcat TO gt_fieldcat.

" Field for Stock Quantity
CLEAR wa_fieldcat.
wa_fieldcat-fieldname  = 'WERKS'.           " Field name from the internal table
wa_fieldcat-seltext_m  = 'Plant'.           " Column header
wa_fieldcat-col_pos    = 4.                 " Position of the column
wa_fieldcat-outputlen  = 10.                " Output length of the column
wa_fieldcat-do_sum     = 'X'.     " Allows summation for this field
APPEND wa_fieldcat TO gt_fieldcat.

" Display the ALV report
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    it_fieldcat   = gt_fieldcat
  TABLES
    t_outtab      = gt_materials
  EXCEPTIONS
    program_error = 1
    OTHERS        = 2.

IF sy-subrc <> 0.
  MESSAGE 'Error displaying ALV' TYPE 'E'.
ENDIF. 

In this blog, we learned how to manually create a field catalog and display a simple ALV report using the REUSE_ALV_GRID_DISPLAY function module. Manually building the field catalog gives you flexibility in controlling the display properties and ensures compatibility when REUSE_ALV_FIELDCATALOG_MERGE isn't suitable for your requirements.

 You can extend this ALV report by adding sorting, filtering, and event-handling features for a more interactive user experience.






Post a Comment

Previous Post Next Post