Skip to end of metadata
Go to start of metadata

Following code snippet can be used to demonstrate the generation of simple ALV List and Grid report. The generated report has the following features:

  • Selection Screen to get user input
  • Editable Cells
  • Cell Coloring
  • Report Header
  • Sorting
  • Input data validation at selection screen
  • On double click of sales order number, its details (transaction VA03) will be displayed
    Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
    REPORT  z82122_assign1.
    
    ******** START OF DATA DECLARATION ******************
    TYPE-POOLS: slis.
    TABLES     : vbak,                  "Sales Document Header Data
                 vbap.                  "Sales Document Item Data
    TYPES: BEGIN OF st_outputdata,
            vbeln LIKE vbak-vbeln,
            posnr LIKE vbap-posnr,
            matnr LIKE vbap-matnr,
            vbtyp LIKE vbak-vbtyp,
            vkorg LIKE vbak-vkorg,
            vtweg LIKE vbak-vtweg,
            spart LIKE vbak-spart,
            erdat LIKE vbak-erdat,
            ernam LIKE vbak-ernam,
            lgort LIKE vbap-lgort,
            vstel LIKE vbap-vstel,
            prctr LIKE vbap-prctr,
            aufnr LIKE vbap-aufnr,
            voref LIKE vbap-voref,
            zmeng LIKE vbap-zmeng,
            netpr LIKE vbap-netpr,
            comnt(100) TYPE c,
            cell_color TYPE lvc_t_scol,
          END OF st_outputdata.
    
    DATA: t_outputdata TYPE st_outputdata OCCURS 0 WITH HEADER LINE.
    DATA: v_repid LIKE sy-repid.
    DATA: t_layout TYPE slis_layout_alv,
          t_fieldcatelog TYPE slis_t_fieldcat_alv,
          t_listheader TYPE slis_t_listheader,
          t_events TYPE slis_t_event,
          t_sortinfo TYPE slis_t_sortinfo_alv.
    DATA: ls_cellcolor TYPE lvc_s_scol.
    
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
    SELECT-OPTIONS so_vbeln FOR vbak-vbeln OBLIGATORY.
    SELECT-OPTIONS so_erdat FOR vbak-erdat.
    SELECT-OPTIONS so_ernam FOR vbak-ernam.
    SELECT-OPTIONS so_vbtyp FOR vbak-vbtyp.
    SELECT-OPTIONS so_vkorg FOR vbak-vkorg.
    SELECT-OPTIONS so_vtweg FOR vbak-vtweg.
    SELECT-OPTIONS so_spart FOR vbak-spart.
    SELECTION-SCREEN END OF BLOCK b1.
    
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
    PARAMETERS r_list RADIOBUTTON GROUP g1.
    PARAMETERS r_grid RADIOBUTTON GROUP g1.
    SELECTION-SCREEN END OF BLOCK b2.
    ******** END OF DATA DECLARATION ********************
    
    AT SELECTION-SCREEN.
    ****** Validate Sales Document Number Parameter ******
      SELECT SINGLE * FROM vbak WHERE vbeln = so_vbeln-low.
      IF sy-subrc NE 0.
        SET CURSOR FIELD 'SO_VBELN-LOW'.
        MESSAGE 'Enter valid Sales Document Number.' TYPE 'E'.
      ENDIF.
      SELECT SINGLE * FROM vbak WHERE vbeln = so_vbeln-high.
      IF sy-subrc NE 0.
        SET CURSOR FIELD 'SO_VBELN-HIGH'.
        MESSAGE 'Enter valid Sales Document Number.' TYPE 'E'.
      ENDIF.
    
    AT SELECTION-SCREEN OUTPUT.
      MODIFY SCREEN.
    
    INITIALIZATION.
      v_repid = sy-repid.
    
    END-OF-SELECTION.
      PERFORM build_outputdata.
      PERFORM build_fieldcatelog.
      PERFORM build_eventsinfo.
      PERFORM build_sortinfo.
      PERFORM build_listheader.
      PERFORM build_layout USING t_layout.
    
      IF r_list = 'X'.
        PERFORM display_listreport.
      ELSEIF r_grid = 'X'.
        PERFORM display_gridreport.
      ENDIF.
    
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_OUTPUTDATA
    *&---------------------------------------------------------------------*
    FORM build_outputdata.
      SELECT
        vbak~vbeln
        vbap~posnr
        vbap~matnr
        vbak~vbtyp
        vbak~vkorg
        vbak~vtweg
        vbak~spart
        vbak~erdat
        vbak~ernam
        vbap~lgort
        vbap~vstel
        vbap~prctr
        vbap~aufnr
        vbap~voref
        vbap~zmeng
        vbap~netpr
      INTO CORRESPONDING FIELDS OF TABLE t_outputdata
      FROM vbak INNER JOIN vbap ON vbak~vbeln = vbap~vbeln
      WHERE vbak~vbeln IN so_vbeln AND
            vbak~erdat IN so_erdat AND
            vbak~ernam IN so_ernam AND
            vbak~vbtyp IN so_vbtyp AND
            vbak~vkorg IN so_vkorg AND
            vbak~vtweg IN so_vtweg AND
            vbak~spart IN so_spart.
    
      LOOP AT t_outputdata.
        IF t_outputdata-voref = 'X' .
          ls_cellcolor-fname = 'VOREF'.
          ls_cellcolor-color-col = '6'.
          ls_cellcolor-color-int = '1'.
          ls_cellcolor-color-inv = '0'.
          APPEND ls_cellcolor TO t_outputdata-cell_color.
          IF sy-subrc EQ 0.
            MODIFY t_outputdata.
          ENDIF.
        ELSE.
          ls_cellcolor-fname = 'VOREF'.
          ls_cellcolor-color-col = '5'.
          ls_cellcolor-color-int = '1'.
          ls_cellcolor-color-inv = '0'.
          APPEND ls_cellcolor TO t_outputdata-cell_color.
          IF sy-subrc EQ 0.
            MODIFY t_outputdata.
          ENDIF.
        ENDIF.
      ENDLOOP.
    
    ENDFORM.                    "BUILD_OUTPUTDATA
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_FIELDCATELOG
    *&---------------------------------------------------------------------*
    FORM build_fieldcatelog.
      DATA: ls_fieldcat TYPE slis_fieldcat_alv.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 1.
      ls_fieldcat-fieldname = 'VBELN'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Sales Doc No'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 2.
      ls_fieldcat-fieldname = 'POSNR'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Sales Doc Item'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 3.
      ls_fieldcat-fieldname = 'MATNR'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Material No'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 4.
      ls_fieldcat-fieldname = 'VBTYP'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Doc Category'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 5.
      ls_fieldcat-fieldname = 'VKORG'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Sales Org'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 6.
      ls_fieldcat-fieldname = 'VTWEG'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Dist Channel'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 7.
      ls_fieldcat-fieldname = 'SPART'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Division'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 8.
      ls_fieldcat-fieldname = 'ERDAT'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Doc Date'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 9.
      ls_fieldcat-fieldname = 'ERNAM'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Created By'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 10.
      ls_fieldcat-fieldname = 'LGORT'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Storage Loc'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 11.
      ls_fieldcat-fieldname = 'VSTEL'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Shipping/Receiving Point'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 12.
      ls_fieldcat-fieldname = 'PRCTR'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Profit Center'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 13.
      ls_fieldcat-fieldname = 'AUFNR'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Order No'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 14.
      ls_fieldcat-fieldname = 'VOREF'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Ref Indicator'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 15.
      ls_fieldcat-fieldname = 'ZMENG'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Target Qty'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 16.
      ls_fieldcat-fieldname = 'NETPR'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Net Price'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CLEAR ls_fieldcat.
      ls_fieldcat-col_pos  = 16.
      ls_fieldcat-fieldname = 'COMNT'.
      ls_fieldcat-tabname = 'T_OUTPUTDATA'.
      ls_fieldcat-seltext_m = 'Comments'.
    *  ls_FIELDCAT-EDIT = 'X'.
      APPEND ls_fieldcat TO t_fieldcatelog.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_program_name         = v_repid
          i_internal_tabname     = 't_OUTPUTDATA'
          i_structure_name       = 'ST_OUTPUTDATA'
          i_client_never_display = 'X'
          i_inclname             = v_repid
        CHANGING
          ct_fieldcat            = t_fieldcatelog[]
        EXCEPTIONS
          inconsistent_interface = 1
          program_error          = 2
          OTHERS                 = 3.
    ENDFORM.                    "BUILD_FIELDCATELOG
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_EVENTSINFO
    *&---------------------------------------------------------------------*
    FORM build_eventsinfo.
      DATA: ls_event TYPE slis_alv_event.
    
      CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
        EXPORTING
          i_list_type     = 0
        IMPORTING
          et_events       = t_events
        EXCEPTIONS
          list_type_wrong = 1
          OTHERS          = 2.
    
      READ TABLE t_events WITH KEY name = slis_ev_top_of_page INTO ls_event.
      IF sy-subrc = 0.
        MOVE 'TOP_OF_PAGE' TO ls_event-form.
        APPEND ls_event TO t_events.
      ENDIF.
    
      READ TABLE t_events WITH KEY name = slis_ev_user_command INTO ls_event.
      IF sy-subrc = 0.
        MOVE 'USER_COMMAND' TO ls_event-form.
        APPEND ls_event TO t_events.
      ENDIF.
    
    ENDFORM.                    "BUILD_EVENTSINFO
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_LAYOUT
    *&---------------------------------------------------------------------*
    FORM build_layout USING ls_layout TYPE slis_layout_alv.
      ls_layout-coltab_fieldname = 'CELL_COLOR'.
    ENDFORM.                    "BUILD_LAYOUT
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_SORTINFO
    *&---------------------------------------------------------------------*
    FORM build_sortinfo.
      DATA: ls_sort TYPE slis_sortinfo_alv.
    
      ls_sort-fieldname = 'VBELN'.
      ls_sort-spos = 1.
      ls_sort-up = 'X'.
      APPEND ls_sort TO t_sortinfo.
    
      CLEAR ls_sort.
      ls_sort-fieldname = 'POSNR'.
      ls_sort-spos = 2.
      ls_sort-up = 'X'.
      APPEND ls_sort TO t_sortinfo.
    ENDFORM.                    "BUILD_SORTINFO
    *&---------------------------------------------------------------------*
    *&      Form  BUILD_LISTHEADER
    *&---------------------------------------------------------------------*
    FORM build_listheader.
      DATA: ls_line TYPE slis_listheader.
    
    ***Header
      CLEAR ls_line.
      ls_line-typ  = 'H'.
      ls_line-info = 'Sales Order Data'.
      APPEND ls_line TO t_listheader.
    
    ENDFORM.                    "BUILD_LISTHEADER
    *&---------------------------------------------------------------------*
    *&      Form  TOP_OF_PAGE
    *&---------------------------------------------------------------------*
    FORM top_of_page.
    
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          it_list_commentary = t_listheader
          i_logo             = 'ENJOYSAP_LOGO'.
    ENDFORM.                    "TOP_OF_PAGE
    *&---------------------------------------------------------------------*
    *&      Form  USER_COMMAND
    *&---------------------------------------------------------------------*
    FORM user_command USING l_ucomm LIKE sy-ucomm l_selfield TYPE slis_selfield.
    *  IF l_UCOMM = '&IC1'.
      IF l_selfield-fieldname = 'VBELN'.
        READ TABLE t_outputdata INDEX l_selfield-tabindex.
        IF sy-subrc = 0.
          SET PARAMETER ID 'AUN' FIELD t_outputdata-vbeln.
          CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
        ENDIF.
        CLEAR: l_ucomm, t_outputdata.
      ENDIF.
    *  ENDIF.
    ENDFORM.                    "USER_COMMAND
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_LISTREPORT
    *&---------------------------------------------------------------------*
    FORM display_listreport.
      CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
        EXPORTING
          i_callback_program = v_repid
          i_structure_name   = 'ST_OUTPUTDATA'
          is_layout          = t_layout
          it_fieldcat        = t_fieldcatelog[]
          it_sort            = t_sortinfo[]
          it_events          = t_events[]
        TABLES
          t_outtab           = t_outputdata
        EXCEPTIONS
          program_error      = 1
          OTHERS             = 2.
    ENDFORM.                    "DISPLAY_LISTREPORT
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_GRIDREPORT
    *&---------------------------------------------------------------------*
    FORM display_gridreport.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program = v_repid
          i_structure_name   = 'ST_OUTPUTDATA'
          is_layout          = t_layout
          it_fieldcat        = t_fieldcatelog[]
          it_sort            = t_sortinfo[]
          it_events          = t_events[]
        TABLES
          t_outtab           = t_outputdata
        EXCEPTIONS
          program_error      = 1
          OTHERS             = 2.
    
    ENDFORM.                    "DISPLAY_GRIDREPORT