Creating a Function module in ABAP to take any table and write it to the screen

SAP List Viewer (ALV) is used to add an ALV component and provides a flexible environment to display lists and tabular structure. A standard output consists of header, toolbar, and an output table. The user can adjust the settings to add column display, aggregations, and sorting options using additional dialog boxes.

Creating a Function Module Using ALV

You can use the following code to display any table using the CL_SALV_TABLE class ?

DATA: go_alv TYPE REF TO cl_salv_table.
  CALL METHOD cl_salv_table=>factory
    IMPORTING
      r_salv_table = go_alv
    CHANGING
      t_table      = itab.
  go_alv->display( ).

Dynamic Output Using Field-Symbols

Another dynamic way to output any internal table is by using field-symbols. Field-symbols are a particular field type in ABAP that work like pointers without pointer arithmetic, but have value semantics. This approach allows you to process any table structure dynamically.

First, declare the field-symbols with ANY typing ?

FIELD-SYMBOLS: <row> TYPE ANY.
FIELD-SYMBOLS: <comp> TYPE ANY.

The typing ANY is necessary because the field-symbol should be able to refer to data of any type. The loop using dynamic assignment to the various components of the work area looks like this ?

LOOP AT itab_flight INTO <row>.
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE <row> TO <comp>.
    IF sy-subrc <> 0.
      SKIP.
      EXIT.
    ENDIF.
    WRITE <comp>.
  ENDDO.
ENDLOOP.

Complete Function Module Example

Here is a complete function module that can display any internal table ?

FUNCTION z_display_any_table.
*"----------------------------------------------------------------------
*" IMPORTING
*"   REFERENCE(IT_TABLE) TYPE STANDARD TABLE
*"----------------------------------------------------------------------
  DATA: go_alv TYPE REF TO cl_salv_table.
  
  TRY.
    CALL METHOD cl_salv_table=>factory
      IMPORTING
        r_salv_table = go_alv
      CHANGING
        t_table      = it_table.
    go_alv->display( ).
  CATCH cx_salv_msg.
    MESSAGE 'Error displaying table' TYPE 'E'.
  ENDTRY.
ENDFUNCTION.

Conclusion

Both ALV and field-symbols provide flexible ways to create reusable function modules for displaying any table structure dynamically in ABAP, with ALV being more user-friendly and field-symbols offering greater control over data processing.

Updated on: 2026-03-13T19:15:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements