Skip to Content

Scenario: Sometimes you may come across the requirement that there is a ALV table that displays the sales order header, on click of the sales order it should display the all item details but all the materials should display in the same row not in different rows.

For Eg:

Sales order header:

Sales order number
date of creation
Time
Created by

0000004971

14.05.2013 12.45.00 BOLLINGER
0000004972 14.05.2013 12.45.00 BOLLINGER

0000004973

14.05.2013 12.45.00 BOLLINGER
0000004974 14.05.2013 12.45.00 BOLLINGER

Sales order item :

Sales Document Sales Document Item Material Number
0000004973 10 M-05
0000004973 20 M-06
0000004973 30 M-07
0000004973 40 M-08

But my requirement is all the materials of the sales order should be displayed in the same row as below in ALV

Sales Document Material 1 Material 2
Material 3
Material 4
0000004973 M-05 M-06 M-07 M-08

In the above case declaring static internal table for field catalog is not useful.Here every thing should be dynamic.Below is the source code

*&———————————————————————*
*& Report  ZR_DYNAMIC_ALV
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

REPORT  ZR_DYNAMIC_ALV.

type-pools slis.

types: BEGIN OF TY_VBAK,
         VBELN TYPE VBAK-VBELN,
         ERDAT TYPE VBAK-ERDAT,
         ERZET TYPE VBAK-ERZET,
         ERNAM TYPE VBAK-ERNAM,
       END OF TY_VBAK.

types: BEGIN OF TY_VBAP,
         VBELN TYPE VBAP-VBELN,
         POSNR TYPE VBAP-POSNR,
         MATNR TYPE VBAP-MATNR,
         MATWA TYPE VBAP-MATWA,
       END OF TY_VBAP.

data lt_vbak type table of ty_vbak.
data lt_vbap type table of ty_vbap.
data ls_vbap type  ty_vbap.

data lt_fcat type table of slis_fieldcat_alv.
data ls_fcat type slis_fieldcat_alv.

ls_fcat-fieldname = ‘VBELN’.
ls_fcat-tabname = ‘LT_VBAK’.
ls_fcat-seltext_l = ‘Sales order number’.

append ls_fcat to lt_fcat.
clear ls_fcat.

ls_fcat-fieldname = ‘ERDAT’.
ls_fcat-tabname = ‘LT_VBAK’.
ls_fcat-seltext_l = ‘Created on’.

append ls_fcat to lt_fcat.
clear ls_fcat.

ls_fcat-fieldname = ‘ERZET’.
ls_fcat-tabname = ‘LT_VBAK’.
ls_fcat-seltext_l = ‘Creation time’.

append ls_fcat to lt_fcat.
clear ls_fcat.

ls_fcat-fieldname = ‘ERNAM’.
ls_fcat-tabname = ‘LT_VBAK’.
ls_fcat-seltext_l = ‘Created by’.

append ls_fcat to lt_fcat.
clear ls_fcat.

select * from vbak into corresponding fields of table lt_vbak up to 10 rows.

data lt_event type table of slis_alv_event.
data ls_event type slis_alv_event.

ls_event-name = ‘USER_COMMAND’.
ls_event-form = ‘ON_CLICK’.

append ls_event to lt_event.
clear ls_event.

*data r_ucomm type sy-ucomm.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
   EXPORTING
     i_callback_program      = sy-repid
     IT_FIELDCAT   = lt_fcat
     IT_EVENTS     = lt_event
*    I_callback_user_command = ‘ON_CLICK’
   TABLES
     T_OUTTAB      = lt_vbak
   EXCEPTIONS
     PROGRAM_ERROR = 1
     OTHERS        = 2.

IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*&———————————————————————*
*&      Form  on_click
*&———————————————————————*
*       text
*———————————————————————-*
*      –>R_UCOMM      text
*      –>RS_SELFIELD  text
*———————————————————————-*
form on_click USING r_ucomm LIKE sy-ucomm
                   rs_selfield TYPE slis_selfield.
   case r_ucomm.

     when ‘&IC1’.
* Get the data based on the field you clicked
       select * from vbap into corresponding fields of table lt_vbap where vbeln = rs_selfield-value.

       data lt_fcat1 TYPE TABLE OF LVC_S_FCAT. ” For temporary field catalog
       data ls_fcat1 TYPE LVC_S_FCAT.

       data lv_lines type char2.
       data lv_index type char2.

       describe table lt_vbap lines lv_lines.

       types:begin of ty_dynst,
             fname type char10,
       end of ty_dynst.

*      data lt_dynst type table of ty_dynst.
       data ls_dynst type ty_dynst.

       field-symbols:<t_dyntable>  type index table,
                     <fs_dyntable> type any.

       ls_fcat1-fieldname = ‘VBELN’.
       append ls_fcat1 to lt_fcat1.
       clear ls_fcat1.

* Build temporary field catalog to create dynamic internal table
       do lv_lines times.

         lv_index = sy-index.
         concatenate ‘MATNR’ lv_index into ls_fcat1-fieldname.

         append ls_fcat1 to lt_fcat1.
         clear ls_dynst.
         clear lv_index.

       enddo.

       DATA:t_newtable  TYPE REF TO data.
       DATA: t_newline  TYPE REF TO data.
       data lv_tabix    type sy-tabix.
       field-symbols   <fs_test> type any.
* Create Dynamic internal table
       CALL METHOD cl_alv_table_create=>create_dynamic_table     “Here creates the internal table dynamcally
           EXPORTING
             it_fieldcatalog = lt_fcat1
           IMPORTING
             ep_table        = t_newtable.

* Assign the field symbol with dynmica internal table
       ASSIGN t_newtable->* TO <t_dyntable>.

*Create dynamic work area and assign to FS

       CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
       ASSIGN t_newline->* TO <fs_dyntable>.

* MOve the internal table data to field symbol
       loop at lt_vbap into ls_vbap.

         lv_tabix = sy-tabix + 1.
         if sy-tabix = ‘1’.
           assign component sy-tabix  of structure <fs_dyntable> to <fs_test>.
           move ls_vbap-vbeln to <fs_test>.
           assign component lv_tabix of structure <fs_dyntable> to <fs_test>.
           move ls_vbap-matnr to <fs_test>.
         else.
           assign component lv_tabix of structure <fs_dyntable> to <fs_test>.
           move ls_vbap-matnr to <fs_test>.
         endif.
         at end of vbeln.
           append <fs_dyntable> to <t_dyntable>.
         endat.

       endloop.

* Move the fieldsymbol data to standar internal table as REUSE_ALV_GRID_DISPLAY FM only allows standard field symbol type as OUTPUT
       field-symbols <fs_stdtab> type standard table.
       ASSIGN t_newtable->* TO <fs_stdtab>.
       <fs_stdtab>[] = <t_dyntable>[].

* building new field catalog

       data lt_fieldcat type table of slis_fieldcat_alv.
       data ls_fieldcat type  slis_fieldcat_alv.

       loop at lt_fcat1 into ls_fcat1.
         ls_fieldcat-seltext_l = ls_fcat1-fieldname.
         move ls_fcat1-fieldname to ls_fieldcat-fieldname.

         append ls_fieldcat to lt_fieldcat.
       endloop.

* display the data
       CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
         EXPORTING
           IT_FIELDCAT   = lt_fieldcat
         TABLES
           T_OUTTAB      = <fs_stdtab>
         EXCEPTIONS
           PROGRAM_ERROR = 1
           OTHERS        = 2.

   endcase.
endform.                    “on_click

Save activate and execute

Sales order item details for 0000004973

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Suhas Saha

    Have you heard about SALV & RTTS classes?

    Moreover did you even check if the knowledge you are sharing has already been shared in the forums? Just search for “Dynamic ALV” & see the number of hits.

    Next time please think before sharing such outdated & easily available document.

    – Suhas

    (0) 

Leave a Reply